#!/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";
Updated : Fixed a bug, added 7 to show the problem (the previous version did not delete 7 because there was a lesser value).
In reply to Re: How do I remove the duplicates from the array?
by choroba
in thread How do I remove the duplicates from the array?
by PetreAdi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |