Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How do I remove the duplicates from the array?

by Anonymous Monk
on Sep 25, 2013 at 22:55 UTC ( #1055755=note: print w/ replies, xml ) Need Help??


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


Comment on Re: How do I remove the duplicates from the array?
Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055755]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (18)
As of 2015-07-07 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls