C++ Reference ยท Algorithms
The std::unique algorithm
Reference for C++ std::unique and unique_copy: remove consecutive duplicates, the sort-then-unique-then-erase pattern, custom predicates and the new logical end.
std::unique removes consecutive duplicate elements from a range. It keeps the first element of each run of equal values and moves the survivors to the front. Like std::remove, it cannot change the container's size: it returns an iterator to the new logical end, and the elements after it are valid but unspecified.
Because it only compares neighbors, unique removes all duplicates only when equal elements are adjacent, typically after sorting. The standard recipe is sort, then unique, then erase the tail. std::unique_copy writes the result to another range instead of modifying the source.
std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end());
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class FwdIt> FwdIt unique(FwdIt first, FwdIt last); template<class FwdIt, class BinPred> FwdIt unique(FwdIt first, FwdIt last, BinPred p); template<class InIt, class OutIt> OutIt unique_copy(InIt first, InIt last, OutIt d_first); template<class InIt, class OutIt, class BinPred> OutIt unique_copy(InIt first, InIt last, OutIt d_first, BinPred p);
| Name | Description |
|---|---|
first, last |
The range to process. |
p |
Binary predicate; two neighbors for which it returns true count as duplicates. Default is ==. |
d_first |
Where unique_copy writes its output. |
Returns: unique returns the new logical end; the elements in [new_end, last) are valid but unspecified. unique_copy returns the end of the written output.
Errors / undefined behavior: p must be an equivalence relation, otherwise the result is not meaningful. An output range that is too short is undefined.
Usage notes
- Only neighbors are compared.
{1, 2, 1}is left unchanged. Sort first, or use astd::set/unordered_setwhen you do not need to keep the original order. - Does not erase. Always follow with
container.erase(new_end, container.end()). The free functionstd::eraseremoves by value, so it cannot replace this step; see the erase-remove idiom. - Which duplicate survives: the first of each run, so with a custom predicate the survivor is the earliest element.
- Complexity: exactly
last - first - 1comparisons, O(n), plus the sort if you need one. - Order of elements that remain is preserved.
std::listandstd::forward_listhave a memberunique()that removes nodes directly and does shrink the list.- Ranges (C++20):
std::ranges::uniquereturns a subrange for the tail andstd::ranges::unique_copya result struct;std::views::chunk_by(C++23) can group instead of removing. - Parallel overloads accept an execution policy as the first argument.