Tuesday, May 14, 2013

Perl : Remove duplicate elements from arrays



How to remove duplicate element from arrays in Perl? Let us see in this article how can duplicates be removed in different ways?

1. Copying distinct elements to new array using grep function:
my @arr=qw(bob alice alice chris bob);
my @arr1;
foreach my $x (@arr){
        push @arr1, $x if !grep{$_ eq $x}@arr1;
}
print "@arr1";
   A loop is run on the array elements. For every element, it is checked to find out whether the element is already present in the new array. If present, it is skipped, else added to the new array.

2. Same way using regex:
my @arr=qw(bob alice alice chris bob);
my @arr1;
foreach my $x (@arr){
        push @arr1, $x if !grep{/^$x$/}@arr1;
}
print "@arr1";
   This method is same as the above except that instead of string comparison, the array element is checked using the regular expression.

3. Using hash:
my @arr=qw(bob alice alice chris bob);
my %h1;
foreach my $x (@arr){
        $h1{$x}=1;
}
@arr=keys%h1;
print "@arr";
   Every element is stored in a hash with key as the array element and value as 1. Since a hash cannot have duplicate keys, after populating the hash, the keys from the hash will consist of distinct array elements.

4.Using hash and map function:
my @arr=qw(bob alice alice chris bob);
my %h1=map{$_=>1}@arr;
@arr=keys%h1;
print "@arr";
  Same as last example, instead of running a loop on the array elements, the hash is populated using the map function where the array element is the key and the value is 1. Map function returns a key-value pair for every array element where the key is the array element and the value is 1.

5. Using hash in array context for keys:
my @arr=qw(bob alice alice chris bob);
my %h1;
@h1{@arr}=(1..@arr);
@arr=keys%h1;
print "@arr";
  The hash is populated little differently here. Hash can be populated either for a single key-value or for multiple key value pairs. @h1{@arr}=(1..@arr) translates to @h1{"bob","alice","alice","chris","bob"}=(1,2,3,4,5)

No comments:

Post a Comment