Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created September 1, 2025 23:29
Show Gist options
  • Save jpalala/a9a4d21d27488fdd787ed35d77ffc687 to your computer and use it in GitHub Desktop.
Save jpalala/a9a4d21d27488fdd787ed35d77ffc687 to your computer and use it in GitHub Desktop.
remove duplicates c++

Using C++ how do you remove duplicates from a vector set

To remove duplicates from a std::vector in C++, the most common and efficient approach involves using std::sort and std::unique from the header, followed by std::vector::erase. This method modifies the original vector and sorts its elements.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

vector<int> removeDuplicates(vector<int> arr){
  sort(arr.begin(), arr.end());
    arr.erase(unique(arr.begin(), arr.end()), arr.end());
    return arr;
    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment