If order need not be preserved I think a hash slice is quicker.
$ perl -MBenchmark=cmpthese -Mstrict -wE ' > my @arr; > push @arr, int rand 20 for 1 .. 1000; > cmpthese( > -5, > { > grep => sub > { > my %seen; > my @uniq = grep ! $seen{ $_ } ++, @arr; > }, > slice => sub > { > my %seen; > @seen{ @arr } = ( 1 ) x @arr; > my @uniq = keys %seen; > }, > } > );' Rate grep slice grep 3041/s -- -29% slice 4260/s 40% -- $
However, take my benchmarks with a pinch of salt, I often make a complete dog's breakfast of them :-(
I hope this is of interest.
Cheers,
JohnGG
In reply to Re^2: Can't remove the the duplicate element when it's the last one in the array
by johngg
in thread Can't remove the the duplicate element when it's the last one in the array
by littlehorse
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |