In C++, std::list::unique() erases elements that are equal to the element right before them. From each group of equal neighbours, only the first element is kept.
It only looks at neighbours. To remove all duplicates, call sort() first so that equal values sit next to each other. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call unique with empty brackets, or give your own test for “these two are the same”.
C++
l.unique(); // neighbours with ==
l.unique([](int a, int b) { return std::abs(a) == std::abs(b); });
auto n = l.unique(); // C++20: count erased
Call
What it does
l.unique()
Erases every element that is equal (==) to the element before it.
l.unique(pred)
Uses your function pred(a, b) to decide if two neighbours are the same.
auto n = l.unique()
In C++20 and later, the number of erased elements is returned.
Without sorting, 1 2 1 stays 1 2 1, because the two 1s are not neighbours.
Library declarations
C++
void unique(); // before C++20
size_type unique(); // since C++20
template<class BinaryPredicate>
void unique(BinaryPredicate p); // before C++20
template<class BinaryPredicate>
size_type unique(BinaryPredicate p); // since C++20
Parameters
Parameter
Description
p
Optional. A function that takes two neighbouring elements and returns true when they count as equal. The second element is then erased. Without it, == is used.
Return Value
Return Value
Description
size_type (C++20)
The number of elements that were erased. Before C++20 the function returns void.
C++ Examples
Example 1 — unique() Removes Repeated Neighbours
Only elements that touch each other are merged. The final group of 1 values stays, because it is not next to the first 1.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, 1, 2, 2, 2, 3, 1, 1};
l.unique();
for (int n : l) {
std::cout << n << ' ';
}
std::cout << "(size " << l.size() << ")\n";
return 0;
}
Output
1 2 3 1 (size 4)
How It Works
The list has four groups of equal neighbours: 1 1, 2 2 2, 3, and 1 1.
unique() keeps the first element of every group and erases the rest.
The final 1 stays, because it is not next to the first 1. The result is 1 2 3 1.
Example 2 — sort() and unique() Together
To remove all duplicates, sort the list first so equal values become neighbours.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {5, 3, 5, 1, 3, 5};
l.sort(); // 1 3 3 5 5 5
l.unique();
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 3 5
How It Works
sort() turns 5 3 5 1 3 5 into 1 3 3 5 5 5. Now every repeated value sits next to its twins.
unique() erases the second 3 and the second and third 5.
Only one copy of each value is left: 1 3 5. This pair of calls is the standard way to remove duplicates from a list.
Example 3 — unique() with a Predicate
Treat 1 and -1 as the same number by comparing absolute values.
C++
#include <cstdlib>
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, -1, 2, 2, -2, 3};
l.unique([](int a, int b) { return std::abs(a) == std::abs(b); });
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 3
How It Works
The lambda gets two neighbours and says they are “the same” when their absolute values are equal.
1 and -1 match, and 2 2 -2 all match, so only the first of each group is kept.
The list becomes 1 2 3. A plain unique() would have kept -1 and -2 because 1 == -1 is false.
Frequently Asked Questions
It erases consecutive duplicate elements. From every group of equal neighbours, only the first element stays.
Call l.sort() first, then l.unique(). Sorting makes equal values neighbours, and unique() then erases the repeats.
It keeps the first element of each group of equal neighbours and erases the following ones.
std::unique() from <algorithm> only moves elements and returns a new end, so you must call erase() too. list::unique() erases the elements itself.
In C++20 and later it returns the number of erased elements. In C++17 and older it returns void.