How to Find Duplicates in a Vector in C++?
Last Updated :
26 Nov, 2024
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 <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());
int i = 1;
while (i < v.size()) {
bool flag = false;
// Find duplicates
while (i < v.size() && v[i] == v[i - 1]) {
i++;
flag = true;
}
if (flag)
cout << v[i - 1] << " ";
i++;
}
return 0;
}
Explanation: Vector v is first sorted to group identical elements together. Then by iterating through the vector, we compared each element with the previous one and if they are equal, the element is identified as a duplicate and printed.
There are also some other methods in C++ to find the duplicates in a vector. Some of them are as follows:
Using unordered_map
The unordered_map can be used to find the duplicates in a vector by counting the frequency of each elements.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 2, 5, 4, 3, 5, 4};
unordered_map<int, int> um;
// Count frequency of every elements
for (auto i : v)
um[i]++;
for (auto i : um) {
// Check frequency of elements
if (i.second > 1)
cout << i.first << " ";
}
return 0;
}
Explanation: In this method, after counting the frequency of every elements, we check if the frequency of any element is greater than 1. If it is, that means the element is duplicates.
Using unordered_set
Iterate through the vector and keep the track of the elements visited in an unordered_set. If an element already exists in the unordered_set, it's a duplicate.
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, us1;
// Keep track of every elements
for (auto i : v) {
// Check if element already present or not in us
if (us.find(i) != us.end())
cout << i << " ";
else
us.insert(i);
}
return 0;
}
Using Nested Loop
The two loops nested inside one another can be used to find duplicates from the vector. The first loop to iterate over a vector and for each element in the vector, the second loop iterator over the vector to check if this element exists somewhere else or not.
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;
for (int i = 0; i < v.size(); i++) {
// Check for duplicates
for (int j = i + 1; j < v.size(); j++) {
if (v[j] == v[i])
cout << v[i] << " ";
}
}
return 0;
}
Similar Reads
How to Remove Duplicates from a Vector in C++? 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+
3 min read
How to Declare a Vector in C++? In C++, the vector is a dynamic array that can resize itself automatically to accommodate new elements. In this article, we will learn how to declare a vector in C++. Declare a Vector in C++In C++, vectors are defined as the class templates in <vector> header file. So, to declare a std::vector
2 min read
How to Find the Capacity of a Vector in Bytes in C++? In C++, vectors are used to store the collection of similar types of data and are able to automatically resize themself in order to accommodate more data. In this article, we will discuss how to calculate the capacity of a vector in bytes. For Example Input: myVector = {10, 34, 12, 90, 1}; Output: 2
2 min read
How to Find the Size of a Vector in Bytes in C++? In C++, Vectors are dynamic containers that can change their size during the insertion or deletion of elements. In this article, we will explore how we can find the size of the vector in bytes in C++. Example: Input:myVector = {10,20,30,40,50}Output:Size of the vector in bytes is : 20 bytesFind the
2 min read
How to Find the Size of a Vector in C++? The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s
2 min read