cpan beginner

How to use List::MoreUtils uniq to remove duplicates in Perl?

Question

How to use List::MoreUtils uniq to remove duplicates in Perl?

Removing Duplicates from an Array Using List::MoreUtils::uniq

The List::MoreUtils module provides several useful utilities for list manipulation, including the uniq function which can be used to remove duplicate elements from an array. It offers a simplified and efficient way to get unique values while preserving the order of their first occurrence.

What is uniq in List::MoreUtils?

uniq takes a list and returns a new list containing only the first occurrence of each distinct element, effectively removing duplicates. Unlike uniq in some other modules, it preserves the original order of elements.

Basic Usage

  • Import uniq from List::MoreUtils.
  • Pass your list (array) to uniq.
  • Assign the returned list to a new array or overwrite the existing one.

This requires you have List::MoreUtils installed. It is a core module bundled with Perl since 5.10, so you likely have it already.

Example Code

use strict;
use warnings;

# Import uniq from List::MoreUtils (core module since Perl 5.10)
use List::MoreUtils qw(uniq);

# Example array with duplicates
my @data = qw(apple banana apple orange banana grape apple);

# Remove duplicates, preserving first occurrence order
my @unique = uniq @data;

print "Original list: @data\n";
print "Unique list: @unique\n";

Explanation

  • qw() is a convenient way to create a list of strings.
  • uniq @data processes the list and returns only unique values.
  • The result is stored in @unique and printed out.

Output

When you run the above Perl script, you'll get:

Original list: apple banana apple orange banana grape apple
Unique list: apple banana orange grape

Perl Concepts Demonstrated

  • Sigils: @ sigil denotes arrays (@data, @unique).
  • Context: uniq expects a list, returns a list, so scalar vs. list context matters.
  • Modules and Importing: You import uniq explicitly to use it. This is common CPAN practice.

Common Pitfalls

  • Make sure to import uniq properly or you’ll get an error.
  • List::MoreUtils::uniq removes duplicates based on string equality, not deeper data structures or references.
  • Preserved order is based on first occurrence only — last occurrences are discarded.
  • Don’t confuse List::MoreUtils::uniq with List::Util::uniq, which appeared later with different behavior in Perl 5.36+.

Summary

Using List::MoreUtils::uniq is a clean, simple way to remove duplicates from an array in Perl while preserving order. It’s widely available on modern Perl installations and is perfect for beginners learning list manipulation.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 59ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Original list: apple banana apple orange banana grape apple
Unique list: apple banana orange grape
STDERR
(empty)

Was this helpful?

Related Questions