Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: removing duplicated elements from array, with a difference

by Aristotle (Chancellor)
on Aug 28, 2002 at 19:45 UTC ( [id://193558]=note: print w/replies, xml ) Need Help??


in reply to removing duplicated elements from array, with a difference

One and a half passes.
my ($i, %seen) = (0); my @no_dup = grep { 2 < push @{$seen{/(pattern)/}}, $i++ } @with_dup; @no_dup = map { 2 < @$_ ? $no_dup[$_->[0]] : () } values %seen;
Makes use of the little known return value from push but probably no more efficient than the two pass solutions due to creation of a lot of anonymous arrays. Hmm.. I can do better..
my ($i, %seen) = (0); my @no_dup = grep { -1 != ($seen{/(pattern)/} = exists $seen{$1} ? -1 : $i++) } @with_dup; @no_dup = @no_dup{grep $_ != -1, values %seen};
Update: fixed a thinko.

Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found