Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to find and remove duplicate elements from an array?

by jdporter (Paladin)
on Jul 20, 2007 at 14:33 UTC ( [id://627795]=note: print w/replies, xml ) Need Help??


in reply to How to find and remove duplicate elements from an array?

The answers in the FAQ don't modify the array in-place. In case that's what you need, you can do the following:
my @a = qw( a a b c c c d e f e f e a f g h h h ); my %seen; for ( my $i = 0; $i <= $#a ; ) { splice @a, --$i, 1 if $seen{$a[$i++]}++; } print "@a\n";
This can be wrapped in a sub like so:
sub remove_duplicates(\@) { my $ar = shift; my %seen; for ( my $i = 0; $i <= $#{$ar} ; ) { splice @$ar, --$i, 1 if $seen{$ar->[$i++]}++; } } my @a = qw( a a b c c c d e f e f e a f g h h h ); remove_duplicates( @a ); print "@a\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-23 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found