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

Your code seems to be too "tricky" (read: complicated)
Especially, you should avoid changing variables more than once in a statement. E.g. using --$i and $i++ in
splice @$arr, --$i, 1 if $seen{$arr->[ $i++]}++;

As already recommended by WizardOfUz, you should use uniq(). If you want to do it all by yourself, the following code seems to work;
sub remove_dup { my @arr = (1,1); #,2, 1, 3, 1, 4, 3, 1); my %seen; for ( my $i = 0; $i < scalar @arr; $i++){ splice @arr, $i--, 1 if $seen{$arr[ $i]}++; } print @arr, " "; }

Replies are listed 'Best First'.
Re^2: Can't remove the the duplicate element when it's the last one in the array
by ikegami (Patriarch) on Dec 18, 2009 at 16:05 UTC
    my %seen; for ( my $i = 0; $i < scalar @arr; $i++){ splice @arr, $i--, 1 if $seen{$arr[ $i]}++; }
    can be simplified to
    my %seen; for ( my $i = @arr; $i--; ){ splice @arr, $i, 1 if $seen{$arr[$i]}++; }
    But the following will be much faster:
    my %seen; @arr = grep !$seen{$_}++, @arr;
Re^2: Can't remove the the duplicate element when it's the last one in the array
by littlehorse (Sexton) on Dec 18, 2009 at 09:15 UTC

    Ratazong, I have figured out it's my mistake. Yes, It's more readable to write it in your style, many thanks to you.