in reply to Extracting unique elements from array

I did get this from perlmonks, but cant find the reference id:
I am not the author of this snippet, but I use it always:
my %saw; my @array = (1,1,1,2,2,3,3,3,3,4,5,5,5,6,6,7,6,5); @saw{@array} =(); # initialize hash slice my @uniq_elements = sort keys %saw;

Replies are listed 'Best First'.
Re^2: Extracting unique elements from array
by SimonSaysCake (Beadle) on Dec 12, 2015 at 14:26 UTC
    Here's a one-liner (if the original order isn't important) - no temp hash var required:
    @foo = (1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10); my @unique = sort {$a <=> $b} keys({map {$_ => 1} @foo}); print join(",", @unique) . "\n";
    Two or more arrays? Not a problem.
    @foo = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); @bar = (0, 1, 2, 5, 7, 9, 11, 12, 13, 14); my @unique = sort {$a <=> $b} keys({map {$_ => 1} (@foo, @bar)}); print join(",", @unique) . "\n";
    -Simon