How can I extract just the unique elements of an array?
Years ago i wrote a subroutine to find the unique elements of an Array.
The code was the following
sub del_double{ my %all; grep {$all{$_}=0} @_;
return (keys %all);
}
How does it work?
Well, first the hash %all is defined.
With the grep-function I set the keys of %all with all of the elements of myarray. Double keys are not possible, so i have the unique elements in (keys %all).
This funtion ist very fast, and it found it's place in my private perl-library.
A faster method is the following:
sub del_double
{ my %all;
$all{$_}=0 for @_;
return (keys %all);
}
It works with a for-loop, not with grep. And this is the reason, why it is faster than the first method. The mighty grep-function is very powerful with arrays... but slower than a for or a foreach.
Since I began this blog, I've been looking for the very fastest subroutine to solve this problem.
Now, after searching the internet, I found the (I think) fastes way to find the unique elements of an array.
The code is
sub del_double{ my %all=();
@all{@_}=1;
return (keys %all);
}
Why is this so fast?
Differnt to my method, this method gives the array DIRECTLY to the hash %all. It needs no grep, for or foreach, and I think, this is the reason, why it is so fast.
But be careful: The chronological order of the returned array is not the same as the given array.
Kommentare zum Beitrag "How can I extract just the unique elements of an array?"
Kommentar von Renée Bäcker
Der Nachteil bei der Methode ist, dass die Reihenfolge der Elemente nicht erhalten bleibt. Dies ist aber oft sehr wichtig. In so einem Fall sollte man so etwas machen:
my %seen;
my @unique = grep{ ! $seen{$_}++ }@array;