in reply to Removing Duplicates in an array?

From the Perl Cookbook:
my @list = qw(some list of of non unique words); my %unique = (); foreach my $item (@list) { $unique{$item}++; }
The keys of %unique are the unique values.

Replies are listed 'Best First'.
Re: Re: Removing Duplicates in an array?
by davorg (Chancellor) on Jun 26, 2001 at 17:13 UTC
      TIMTOWTDI: How about one that preserves the original order?
      @a=qw( one two three for five six seven eight nine ten eleven); @hash{@a}=(0..$#a); @sorted=sort { $hash{$a}<=>$hash{$b} } keys %hash;

      mr.nick ...

      nice solution, but you could still be doing a lot of unnecessary data copying - depending on what is stored in @array - so:

      my %unique; @unique{@list} = (); @list = keys %unique;

      With the cookbook method you have of course the benefit of being able to get a count of the number of occurences ...

      -- Hofmator