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
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals