How to Remove Duplicates from a Vector in C++?
Last Updated :
26 Nov, 2024
In this article, we will learn how to remove the duplicates from the vector in C++.
The recommended way to remove the duplicates from the vector is by using sort() with unique() to move all the duplicate elements at the end and then remove them using vector erase(). Let’s take a look at an example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 2, 5, 4, 3, 5, 4};
// Sort the vector
sort(v.begin(), v.end());
// Move all duplicates to last of vector
auto it = unique(v.begin(), v.end());
// Remove all duplicates
v.erase(it, v.end());
for (auto i : v)
cout << i << " ";
return 0;
}
Explanation: The unique() function moves duplicate elements to the end of the vector and returns an iterator to the new logical end, but it need sorted range, so we first sort the array. Finally, the duplicates are removed using the vector erase() method.
Note: This method changes the order of the elements.
There are also some other methods in C++ by which we can remove the duplicates from the vector. Some of them are as follows:
Using unordered_set
To preserve the order of elements, use unordered_set to store whether the element is encountered or not and remove the copies when same element is encounted.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 2, 5, 4, 3, 5, 4};
unordered_set<int> us;
auto it = v.begin();
while (it != v.end()) {
// If elements exist in us then current
// copy is duplicate, so remove it
if (us.find(*it) != us.end())
it = v.erase(it);
// If elemetn is not present in us,
// then it is unique so insesrt it in us
else {
us.insert(*it);
it++;
}
}
for (auto i : v)
cout << i << " ";
return 0;
}
Using Nested Loop
The duplicate elements can also be removed manually from the vector using nested loop. In this method, manually finding the duplicates from the vector by using two nested loops and erase it using vector erase() method.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 2, 5, 4, 3, 5, 4};
for (int i = 0; i < v.size(); i++) {
// Compare each element with current one
for (int j = i + 1; j < v.size();) {
// Erase if duplicate is found.
if (v[j] == v[i])
v.erase(v.begin() + j);
else
j++;
}
}
for (auto i : v)
cout << i << " ";
return 0;
}
Explanation: This is simplest method to remove the elements, but it is not effective as its time complexity is O(n2) (where n is the number of elements in vector) due to nested loop.
Similar Reads
How to Find Duplicates in a Vector in C++? In this article, we will learn how to find the duplicate elements in a vector in C++.The easiest way to find the duplicate elements from the vector is by using sort() function to first sort the vector and then check the adjacent elements for duplicates. Letâs take a look at an example:C++#include
3 min read
How to Remove an Element from Vector in C++? In this article, we will learn how to remove a given element from the vector in C++.The most straightforward method to delete a particular element from a vector is to use the vector erase() method. Let's look at a simple example that shows how to use this function:C++#include <bits/stdc++.h> u
2 min read
How to Remove Duplicates from a Vector While Preserving Order in C++? A vector in C++ is used for storing values of certain data types in contiguous memory locations like arrays. In this article, we will learn how to remove duplicates from an unsorted vector while keeping the original order of elements in C++. Example Input:myVector = {1, 2, 5, 4, 3, 2, 1, 5, 6, 5}Out
2 min read
How to Remove Last Element from Vector in C++? Given a vector of n elements, the task is to remove the last element from the vector in C++.The most efficient method to remove the last element from vector is by using vector pop_back() method. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() {
2 min read
How to Delete Multiple Elements from a Vector in C++? In this article, we will learn how to erase multiple elements from a vector in C++.The easiest and most efficient method to delete the multiple elements from the vector is by using vector erase() method. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main(
2 min read