Perl Scripts ·
Remove Duplicate Values from an Array in Perl
By Manu Mathew, Technical Lead Engineer · Published 1 April 2021 · Last updated 5 October 2026

Lists collected from forms, files or database rows often contain the same value more than once. Perl has no built-in “unique” operator, but you can remove duplicates from an array in Perl in one line once you know where to look.
I tested every example below on Perl 5.40 with use strict and use warnings.
Quick answer
On Perl 5.26 or later, use uniq from List::Util: my @unique = uniq @list; keeps the first copy of each value, in the original order. On older Perls, use a %seen hash with grep. Use uniqnum when values should be compared as numbers, and lc in the %seen key to ignore case.
How do I remove duplicates with List::Util?
Import uniq from List::Util, a core module, and pass it the array.
use strict;
use warnings;
use List::Util qw(uniq uniqnum);
my @colours = qw(red blue red green blue);
my @unique = uniq @colours; # keeps the first of each value, in order
print "@unique\n"; # red blue green
my @numbers = (3, '3.0', 3, 7);
print join(' ', uniq @numbers), "\n"; # 3 3.0 7 (compared as strings)
print join(' ', uniqnum @numbers), "\n"; # 3 7 (compared as numbers)
uniq compares values as strings, so 3 and 3.0 count as different. uniqnum compares them as numbers. Both arrived in List::Util 1.45; Perl 5.26 and later ship a version that has them.
How does the %seen hash method work?
grep keeps an element only when $seen{$_}++ is still zero, which is true the first time each value appears.
use strict;
use warnings;
sub unique {
my %seen;
return grep { !$seen{$_}++ } @_;
}
my @array = qw(one two three two three);
my @filtered = unique(@array);
print "@filtered\n"; # one two three
print scalar(@filtered), "\n"; # 3
print @filtered . "\n"; # 3 (the original post's bug: . forces scalar context)
The post-increment returns the old count and then adds one, so the first copy sees 0 (kept) and later copies see 1 or more (dropped). The original 2021 version of this note printed the result with print @filtered . "\n", which prints the count, 3, rather than the values: the . operator forces the array into scalar context. Interpolating it, as above, prints the values.
How do I ignore case when removing duplicates?
Use a lower-cased copy of each value as the %seen key, and keep the original value in the result.
use strict;
use warnings;
my @names = qw(Asha asha Ravi ASHA ravi Joe);
my %seen;
my @unique = grep { !$seen{ lc $_ }++ } @names; # ignore case, keep first spelling
print "@unique\n"; # Asha Ravi Joe
What if the order doesn’t matter?
Use the values as hash keys and take the keys back out; sort them if you need a predictable order.
my @list = qw(a b a c b a);
my @unique = sort keys %{ { map { $_ => 1 } @list } };
print "@unique\n"; # a b c
Without the sort, the order of hash keys is random and can change between runs, so prefer uniq when the original order matters.
How do I count how many times each value appears?
Increment a counter per value in a hash, then read the counts back.
my @list = qw(a b a c b a);
my %count;
$count{$_}++ for @list;
print "$_: $count{$_}\n" for sort keys %count;
# a: 3
# b: 2
# c: 1
Frequently asked questions
Does uniq change the original array?
No. uniq returns a new list and leaves the array you pass in unchanged. Assign the result back, as in @list = uniq @list;, if you want to replace the original.
How do I remove duplicates from an array of hashes?
Pick the field that makes a record unique and use it as the %seen key: grep { !$seen{ $_->{email} }++ } @people. For uniqueness across several fields, join them into one key, for example join("\0", @$_{qw(name city)}).
Should I use uniq or the %seen hash?
Both make a single pass through the list, so both cope well with large arrays. uniq is shorter and says what it does. The %seen version works on any Perl and is easy to adapt, for example to ignore case or compare one field.
How do I find the values that appear more than once?
Count them, then keep the ones with a count above one: my %n; $n{$_}++ for @list; my @dupes = grep { $n{$_} > 1 } keys %n;. Sort the result if you need a predictable order.
Related notes: Perl arrays: length, add, remove and search; Perl hashes; sorting arrays in Perl; hiring a remote Perl developer.
Need help with Perl code?
If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.
You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.
An earlier version of this note (2021) showed the %seen helper only, with a printing bug. This version is rewritten with List::Util and tested examples.
Sources: List::Util: uniq and uniqnum; perlfaq4: removing duplicate elements.