THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial explains multiple ways to remove duplicate elements in Perl Array
To remove elements from an array in Perl, Please follow the below steps.
uniq
from the List::MoreUtils
libraryuniq
method with an array of duplicate valuesuse List::MoreUtils qw(uniq);
my @numbers = (1,2,3,1,1,4,3,2);
my @result = uniq(@numbers);
print "@numbers\n";
Output:
1 2 3 4
my @numbers = (1,2,3,1,1,4,3,2);
sub unique {
my %array;
grep !$array{$_}++, @_;
}
my @result = unique(@numbers);
print "@result\n";
This approach, Convert an array into a hash object with keys as array numbers and values and Finally, return keys Since Hash does not allow duplicate values as a key.
map
functionkeys
.Here is an example
my @numbers = (1,2,3,1,1,4,3,2);
my %hashVariable = map { $_ => 1 } @numbers;
my @result = keys %hashVariable;
print "@result";
🧮 Tags
Recent posts
Nodejs package.json resolutions How to find Operating System username in NodeJS? How to convert Double to Integer or Integer to double in Dart| Flutter By Example Ways to skip test case execution in Gradle project build Learn Gradle | tutorials, and examplesRelated posts