This tutorial will discuss about unique ways to remove duplicates from a vector in C++.

Technique 1: Using nested for-loop

Loop through all elements of vector, and for each element at ith index, check if similar element exists from index i+1 till the end of vector, and store these duplicate indices in a set. Then iterate over the indices in set in reverse order, and delete elements at those indices from the vector. It will remove all duplicate elements from the vector. The order of remaining elements will remain unchanged in this technique.

Let’s see the complete example,

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>

int main()
{
    // Vector of integers
    std::vector<int> vecObj {11, 12, 13, 14, 15, 16, 12, 43, 12, 13, 11};

    // A Set to store indices of duplicate elements
    std::set<int> indices;

    // Iterate over all elements of vector, and store indices
    // of duplicate elements in a set
    for(int i = 0; i < vecObj.size(); i++)
    {
        for(int j = i+1; j < vecObj.size(); j++)
        {
            if (vecObj[i] == vecObj[j])
            {
                indices.insert(j);
            }
        }
    }

    // Iterate over the set of indices in reverse order and
    // delete element at that index in vector
    for (auto rit = indices.rbegin(); rit != indices.rend(); rit++)
    {
        // Remove element at the given index
        vecObj.erase(vecObj.begin() + *rit);
    }

    // print all elements of vector
    for(auto elem : vecObj) {
        std::cout<<elem << ", ";
    }

    return 0;
}

Output :

11, 12, 13, 14, 15, 16, 43,

Technique 2: Using std::sort() and std::unique()

First sort the vector using STL Algorithm std::sort() and then shift all the duplicate elements to the end in this sorted vector using std::unique() function. Once all duplicate elements are shifted at one end of vector, then delete them using the vector::erase() function. This way all diplicates will be deleted from the vector. But it will change the order of remaining elements in vector.

Let’s see the complete example,

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    // Vector of integers
    std::vector<int> vecObj {11, 12, 13, 14, 15, 16, 12, 43, 12, 13, 11};

    // Sort the values in vector
    std::sort(vecObj.begin(), vecObj.end());

    // Remove duplicate values from vector
    vecObj.erase(std::unique(vecObj.begin(), vecObj.end()), vecObj.end());

    // print all elements of vector
    for(auto elem : vecObj) {
        std::cout<<elem << ", ";
    }

    return 0;
}

Output :

11, 12, 13, 14, 15, 16, 43,

Technique 3: Using unordered_set

As a set can have only unique elements in C++. So, create a unordered_set, and initialize it with vector elements. This set will have unique elements only. Then replace the contents of vector with contents of set. This way vector will have only unique elements, and all duplicate elements will get deleted from the vector. But it will change the order of remaining elements in vector.

Let’s see the complete example,

#include <iostream>
#include <vector>
#include <unordered_set>
#include <iterator>

int main()
{
    // Vector of integers
    std::vector<int> vecObj {11, 12, 13, 14, 15, 16, 12, 43, 12, 13, 11};

    // Create a set from vector values
    std::unordered_set<int> setObj(vecObj.begin(), vecObj.end());

    // Replace vector values with the set values
    vecObj.assign(setObj.begin(), setObj.end());


    // print all elements of vector
    for(auto elem : vecObj) {
        std::cout<<elem << ", ";
    }

    return 0;
}

Output :

43, 16, 15, 14, 13, 12, 11,

Summary

Today we learned about several ways to remove duplicates from a vector in C++. Thanks.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.