0
0
CppHow-ToBeginner · 3 min read

How to Remove Duplicates from Vector in C++ Quickly

To remove duplicates from a std::vector in C++, first sort the vector using std::sort, then use std::unique to move duplicates to the end, and finally erase them with vector::erase. This method keeps only unique elements in the vector.
📐

Syntax

To remove duplicates from a std::vector, you use three main steps:

  • std::sort(vector.begin(), vector.end()) - sorts the vector so duplicates are next to each other.
  • auto last = std::unique(vector.begin(), vector.end()) - moves duplicates to the end and returns an iterator to the new end of unique elements.
  • vector.erase(last, vector.end()) - removes the duplicate elements from the vector.
cpp
std::sort(vec.begin(), vec.end());
auto last = std::unique(vec.begin(), vec.end());
vec.erase(last, vec.end());
💻

Example

This example shows how to remove duplicates from a vector of integers. It prints the vector before and after removing duplicates.

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

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

    std::cout << "Before removing duplicates: ";
    for (int n : vec) std::cout << n << ' ';
    std::cout << '\n';

    std::sort(vec.begin(), vec.end());
    auto last = std::unique(vec.begin(), vec.end());
    vec.erase(last, vec.end());

    std::cout << "After removing duplicates: ";
    for (int n : vec) std::cout << n << ' ';
    std::cout << '\n';

    return 0;
}
Output
Before removing duplicates: 4 2 5 2 3 4 1 5 After removing duplicates: 1 2 3 4 5
⚠️

Common Pitfalls

Common mistakes when removing duplicates from a vector include:

  • Not sorting the vector before calling std::unique. std::unique only removes consecutive duplicates, so sorting is necessary.
  • Forgetting to erase the duplicates after std::unique. The vector size does not change until you erase the duplicates.
  • Using std::unique on unsorted data leads to unexpected results.
cpp
#include <vector>
#include <algorithm>

// Wrong way: unique without sorting
std::vector<int> v = {3, 1, 2, 3, 2};
auto last = std::unique(v.begin(), v.end());
v.erase(last, v.end()); // duplicates remain because vector is not sorted

// Right way:
std::sort(v.begin(), v.end());
auto last2 = std::unique(v.begin(), v.end());
v.erase(last2, v.end()); // duplicates removed
📊

Quick Reference

Summary tips for removing duplicates from a vector:

  • Always sort the vector first.
  • Use std::unique to rearrange duplicates.
  • Use vector::erase to remove duplicates physically.
  • This method works for any type that supports < comparison.

Key Takeaways

Sort the vector before using std::unique to ensure duplicates are adjacent.
std::unique moves duplicates to the end but does not change vector size.
Erase the duplicates after std::unique to remove them from the vector.
This method works for any sortable vector elements.
For unsorted data, skipping sort leads to incomplete duplicate removal.