Skip to content
C++ Basics: Remove Adjacent Duplicates with unique and erase

C++ Basics: Remove Adjacent Duplicates with unique and erase

November 3, 2026

Collapse repeated neighboring values, then shrink the vector to its logical new end.

Prepare a scratch directory

Tested on Linux x86_64 with GCC 14.2.0. Install a C++20-capable compiler and standard-library development headers, with g++ on PATH. You also need Bash, a text editor, and the mktemp, rm, and rmdir utilities. No third-party library is required.

Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save this as unique-adjacent-duplicates-demo.cpp:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> readings{4, 4, 7, 7, 4, 4};
    const auto logical_end = std::unique(readings.begin(), readings.end());
    std::cout << "Size before erase: " << readings.size() << '\n';
    readings.erase(logical_end, readings.end());
    std::cout << "Adjacent runs:";
    for (int reading : readings) {
        std::cout << ' ' << reading;
    }
    std::cout << '\n';
    std::sort(readings.begin(), readings.end());
    readings.erase(std::unique(readings.begin(), readings.end()), readings.end());
    std::cout << "Distinct sorted values:";
    for (int reading : readings) {
        std::cout << ' ' << reading;
    }
    std::cout << '\n';
}

Compile and run it. The second command runs only after compilation succeeds:

g++ -std=c++20 -Wall -Wextra -Werror -pedantic unique-adjacent-duplicates-demo.cpp -o unique-adjacent-duplicates-demo &&
./unique-adjacent-duplicates-demo

Observed output:

Size before erase: 6
Adjacent runs: 4 7 4
Distinct sorted values: 4 7

Read the result

The first pass leaves 4, 7, 4: the last 4 is not adjacent to the first run. unique compacts retained values but does not change vector size. erase removes the leftover suffix. Sorting before the second pass makes all equal values adjacent, producing 4, 7.

Apply it carefully

Do not inspect the suffix between logical_end and the original end as meaningful results. Its elements remain valid but have unspecified values. Sorting changes order, so use the first approach for run compression and the second only when sorted distinct values are wanted. Any custom equality predicate must define an equivalence relation.

The C++ working draft describes the relevant library rules. This example uses only C++20 facilities, even though the linked draft also documents newer additions.

Clean up

Remove only the source and executable created here, then remove the empty practice directory:

rm -- unique-adjacent-duplicates-demo.cpp unique-adjacent-duplicates-demo
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect its remaining contents before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on