Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Create unique array --the hard way!

by hdb (Prior)
on Mar 07, 2014 at 19:00 UTC ( #1077442=note: print w/ replies, xml ) Need Help??


in reply to Create unique array --the hard way!

How about losing the duplicates while sorting? Here is the quicksort implementation from Rosetta Code:

sub quick_sort { my @a = @_; return @a if @a < 2; my $p = pop @a; quick_sort(grep $_ < $p, @a), $p, quick_sort(grep $_ >= $p, @a); } my @a = (4, 65, 2, -31, 0, 99, 83, 782, 1); @a = quick_sort @a; print "@a\n";

(This is for sorting numbers.) If you now replace >= with > so you get

quick_sort(grep $_ < $p, @a), $p, quick_sort(grep $_ > $p, @a);

you will get a sorted array without duplicates.


Comment on Re: Create unique array --the hard way!
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2015-07-05 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls