Example 1 — Sorted duplicates
Remove repeated values from a sorted vector.
#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
- Equal neighbours form three groups: 1 1, 2 2 2 and 3 3.
unique()keeps the first of each group.erase()removes the leftover tail.

