in reply to Re: Can't remove the the duplicate element when it's the last one in the array
in thread Can't remove the the duplicate element when it's the last one in the array

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

  • Comment on Re^2: Can't remove the the duplicate element when it's the last one in the array
  • Download Code

Replies are listed 'Best First'.
Re^3: Can't remove the the duplicate element when it's the last one in the array
by littlehorse (Sexton) on Dec 20, 2009 at 07:46 UTC
    johngg, it looks interesting and helpful, I will have a try:)