Remove the duplicate data from array using perl

rajeshkumar created the topic: Remove the duplicate data from array using perl

Remove the duplicate data from array using perl

Method 1
============================================
sub uniqueentr
{
return keys %{{ map { $_ => 1 } @_ }};
}

@array = ("perl","php","perl","asp”);

print join(" ", @array), "\n";
print join(" ", uniqueentr(@array)), "\n";

Method 2
============================================
sub uniq {
return keys %{{ map { $_ => 1 } @_ }};
}

@my_array = ("one","two","three","two","three");
print join(" ", @my_array), "\n";
print join(" ", uniq(@my_array)), "\n";

Method 3
============================================
sub uniq2 {
my %seen = ();
my @r = ();
foreach my $a (@_) {
unless ($seen{$a}) {
push @r, $a;
$seen{$a} = 1;
}
}
return @r;
}
@my_array = ("one","two","three","two","three");
print join(" ", @my_array), "\n";
print join(" ", uniq2(@my_array)), "\n";

Method 4
============================================
my %unique = ();
foreach my $item (@myarray)
{
$unique{$item} ++;
}
my @myuniquearray = keys %unique;

Method 5
============================================
sub duplicate {
my @args = @_;
my %items;
for my $element(@args) {
$items{$element}++;
}
return grep {$items{$_} > 1} keys %items;
}

Method 6
============================================
#! /usr/bin/perl -w

use strict;

sub uniq{
my %temp_hash = map { $_, 0 } @_;
return keys %temp_hash;
}

my @test_array = qw/ 1 3 2 4 3 2 4 7 8 2 3 4 2 3 2 3 2 1 1 1 /;
my @uniq_array = uniq(@test_array);
print "@uniq_array\n";

Method 7
============================================
%seen=();
@unique = grep { !$seen{$_} ++ } @array;

Method 8
============================================
my @in=qw(1 3 4 6 2 4 3 2 6 3 2 3 4 4 3 2 5 5 32 3); #Sample data
my @out=keys %{{ map{$_=>1}@in}}; # Perform PFM
print join ' ', sort{$a<=>$b} @out;# Print data back out sorted and in order.

Reference:
Different Source of Google Search

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Rajesh Kumar
Total Page Visits: 60 - Today Page Visits: 1