http://www.perlmonks.org?node_id=1055755


in reply to How do I remove the duplicates from the array?

This one assumes integer values:

use strict; use warnings; # Including choroba's sevens. 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); # Tuple of array elements as "key"/"val" pairs. my @pairs; # Max "val" for each "key". my %max; for (0..$#array) { my $key = $array[$_]; my $val = $array1[$_]; my $max = $max{$key}; push @pairs, [$key, $val]; if (defined $max) { next if $val < $max; # For the "2"/"3" pairs. Only want "highlander" max, so # increase by a fraction (assumes integers in @array1). $val += 0.5 if $val == $max; } $max{$key} = $val; } my (@output, @output1); for (@pairs) { my ($key, $val) = @$_; next unless $val == $max{$key}; push @output, $key; push @output1, $val; } say join ',', map sprintf('%3d', $_), @$_ for \@output, \@output1; __END__ -20, 1, 9, 3, -4, 5, 20 10, 9, 1, 3, 5, 5, 30

Replies are listed 'Best First'.
Re^2: How do I remove the duplicates from the array?
by PetreAdi (Acolyte) on Sep 26, 2013 at 08:54 UTC

    Very good

    Thank you