in reply to Re: Removing Duplicates from Array Passed by Reference
in thread Removing Duplicates from Array Passed by Reference
For this particular problem, I believe that we can manually do the work of splice more efficiently than splice could itself.
# Untested :( sub dedup { my $ref = shift; my %seen; my $c = 0; # current my $n = 0; # next available slot foreach (@$ref) { if (not $seen{$_}++) { $ref->[$n] = $_ unless $n == $c; $n++; } $c++; } $#{$ref} = $n-1; }
Every element gets moved once, at most. Elements before the first duplicate don't get moved at all.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Removing Duplicates from Array Passed by Reference
by BrowserUk (Patriarch) on May 17, 2003 at 04:48 UTC |