C++ std::unique() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::unique() goes through a range and keeps only the first element of each group of equal neighbours. It returns an iterator to the new end.

It only compares neighbours, so sort the data first if you want to remove all duplicates. Like remove(), it does not shrink the container, so call erase() afterwards.

Syntax

Add #include <algorithm>, then call std::unique(). Start with these two lines.

C++
auto newEnd = std::unique(v.begin(), v.end());
v.erase(newEnd, v.end());
CallWhat it does
auto newEnd = std::unique(v.begin(), v.end());Collapse runs of equal neighbours.
v.erase(newEnd, v.end());Delete the leftover tail.
Library declarations
C++
template<class ForwardIt>
ForwardIt unique(ForwardIt first, ForwardIt last);

template<class ForwardIt, class BinaryPred>
ForwardIt unique(ForwardIt first, ForwardIt last, BinaryPred p);

Parameters

ParameterDescription
first, lastRequired. The range to process.
pOptional. A function that says if two neighbours count as equal.

Return Value

Return ValueDescription
IteratorPoints just after the last kept element.

C++ Examples

Example 1 — Sorted duplicates

Remove repeated values from a sorted vector.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 1, 2, 2, 2, 3, 3};

    v.erase(std::unique(v.begin(), v.end()), v.end());

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. Equal neighbours form three groups: 1 1, 2 2 2 and 3 3.
  2. unique() keeps the first of each group.
  3. erase() removes the leftover tail.

Example 2 — Unsorted data

See that only neighbours are compared.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 2, 1, 1, 3};

    v.erase(std::unique(v.begin(), v.end()), v.end());

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The groups of equal neighbours are 1, then 2 2, then 1 1, then 3.
  2. The first 1 and the later 1 are not neighbours, so both stay.
  3. Sort first if you want a result with each value only once.

Example 3 — Custom equality

Treat numbers with the same absolute value as equal.

C++
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {5, -5, -5, 7, -7, 2};

    v.erase(std::unique(v.begin(), v.end(),
                        [](int a, int b) { return std::abs(a) == std::abs(b); }),
            v.end());

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The lambda compares absolute values, so 5 and -5 count as equal.
  2. 5 keeps its place, and the next two -5 values are dropped. Then 7 keeps its place and -7 is dropped.
  3. 2 has no equal neighbour, so it stays.

Frequently Asked Questions

In C++, std::unique() removes consecutive duplicate elements and returns the new end. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the new end of the kept elements. The container size does not change until you call erase().
Sort the vector first, then call erase(std::unique(...), v.end()).
It keeps the first element of each group of equal neighbours.

Next: unique_copy()

Copies a range while collapsing consecutive duplicate elements.

unique_copy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

23 people found this page helpful