Do you know where your variables are? PerlMonks

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

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

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

Create A New User
Node Status?
node history
Node Type: note [id://1055755]
help
Chatterbox?
 [stonecolddevin]: Corion I haven't heard much of his work to be honest. [planetscape]: hello, Corion [Corion]: Hi planetscape! [stevieb]: Corion I like the groundbreaking ones (guitar players). I have the ability to pick up on sounds that are groundbreaking or specific to a person, thanks to my years of doing recording/mixing/ sampling (hip-hop mind you, but years of it... [stevieb]: ...has honed in my skills of recognizing sound [stevieb]: All of the early members are coming out of the woodwork today :) Hey, planetscape [Corion]: This cover version had so much promise but the singing is underwhelming :-/ [stevieb]: erix Thanks! I dislike remakes of songs usually, but my favourite remix of guitar/weeps is Jeff Healey. I'll take a listen to yours [planetscape]: hi stevieb!

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-06-22 21:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (531 votes). Check out past polls.