How Many Possible ways to remove Duplicates in an array using perl?
Method 1:
sub filter {
my %hash;
grep !$hash{$_}++, @_;
}
my @array = qw(e f g h i);
my @final= filter(@array);
Method 2:
use List::MoreUtils qw(uniq);
my @array = qw(1 1 1 2 3 4 4);
my @final= uniq(@array);
Method 3:
my %hash = ();
foreach my $item (@array)
{
$hash{$item}++;
}
my @final = keys %hash;
Method 4:
my @array=qw( 3 2 3 4 4 3 2);
my @final=keys %{{ map{$_=>1}@array}};
print join ' ', sort{$a<=>$b} @final;
Method 5:
%hash=();
my @array = qw(e j g h i);
@final = grep { ! $hash{$_} ++ } @array;
Method 6:
my @array;
my @finals;
foreach my $var ( @array )
{
if (! grep(/$var/, @finals ))
{
push( @finals, $var );
}
}
Method 7:
my @array = qw(e f g h i);
my %hash;
@hash{@array}=(1..@array);
@final=keys%hash;
Method 8:
my @final = keys {map {$_ => 1} @array};
Method 9:
@array = qw(e f g h i);
%hash1 = map { $_ => 1 } @array;
@final = keys %hash1;
Method 10:
my @array = qw(e f g h i);
my %hash;
foreach my $v (@array){
$hash{$v}=1;
}
@final=keys%hash;
kaavannan perl blogspot
Labels: kaavannan perl blogspot, perl remove duplicates from array, perl uniq