Open In App

How to Find Duplicates in a Vector in C++?

Last Updated : 26 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
2 4 5 

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;
}

Output
2 5 4 

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;
}

Output
4 5 2 

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;
}

Output
2 5 4 

Next Article
Article Tags :
Practice Tags :

Similar Reads