#!/usr/bin/perl use warnings; use strict; my @array = qw(-20 20 1 1 2 2 2 9 3 -4 -4 5 -20 20 7 7 7); my @array1 = qw( 10 11 7 9 3 3 3 1 3 4 5 5 1 30 8 7 8); my %hash; for my $i (0 .. $#array) { my $first = $array[$i]; my $second = $array1[$i]; if (not exists $hash{$first}{highest}) { # The key seen for the first time. $hash{$first}{highest} = $second; } else { # The highest value seen again. $hash{$first}{delete} = 1 if $second == $hash{$first}{highest}; # A new highest value. if ($second > $hash{$first}{highest}) { $hash{$first}{highest} = $second; $hash{$first}{delete} = 0; } } } # Delete repeated max values. for my $key (keys %hash) { delete $hash{$key} if $hash{$key}{delete}; } my (@output, @output1); for my $i (0 .. $#array) { if (exists $hash{ $array[$i] } and $array1[$i] == $hash{ $array[$i] }{highest}) { push @output, $array[$i]; push @output1, $array1[$i]; } } print "@output\n@output1\n";