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
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts