Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: weird array processing question

by Gulliver (Monk)
on May 05, 2011 at 21:45 UTC ( [id://903252]=note: print w/replies, xml ) Need Help??


in reply to weird array processing question

From your example array it looks like you mean you want to find out if there are 3 duplicated elements in the array. You have 1,2,3 showing up twice. An easy way to do that is to make a hash using the array elements as the keys and increment the hash value for each key as seen. This shows up in the source code for List::Compare as a 'seen' hash.

my %seen; $seen{$_}++ foreach @array_whatsit;

Then count the number of keys in the hash with hash values greater than one.

my @duplicates = grep {$_ if $seen{$_}>1} keys %seen; print "There are ", @duplicates+0, " duplicates.\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found