Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: How do I find if an array has duplicate elements, if so discard it?

by rjimlad (Acolyte)
on Jul 19, 2002 at 20:18 UTC ( [id://183435]=note: print w/replies, xml ) Need Help??


in reply to How do I find if an array has duplicate elements, if so discard it?

Err, okay, my answer, short and sweet: use a hash and array slices:
my @a=(1 .. 2); my @b=(2 .. 3); my %c; @c{@a,@b}=(@a,@b); warn "Duplicates exist!" if scalar @c{@a,@b} != (@a+@b);
Of course, in this instance you don't want the last line - instead you'd probably want something like:
my @d=keys %c;
...which will be out-of-order, but guaranteed duplicate free.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found