Using perl to remove duplicates from an array

perl

I needed to remove duplicate elements from an array in Perl. The internet has a range of different ways to do this, but I found the best and quickest way to do so was to use a temporary hash.

push @data, $some_fancy_variable;
my %temp_hash = map { $_, 0 } @data;
@unique = keys %temp_hash;

Leaving the new @unique array with the duplicates removed.

 

Leave a Reply

Your email address will not be published. Required fields are marked *