STL Algorithm std::unique Tutorial

STL Algorithm ( std :: unique)

This article is all about std :: unique algorithm. This std :: unique is an  Algorithm that removes all consecutive duplicate elements from a given range. For example, we have an array of elements i.e. ,
1,2,3,3,3,4,4,5,5
and we want to delete the conjugative elements i.e. 3 , 4 and 5 . this can be done by following method.

Let’s see the below examples to understand the concept well.

Example 1: Removing element by conjunctive position –

std::unique  removes consecutive duplicate elements. For executing this  we should sort the given range, and  make sure that duplicate elements are at their  consecutive positions.

#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
    std::vector<int> vec1 = {1,6,3,5,4,8,5,2,3,9,8,6,7,1,5,9,0};
    // Sorting of element to bring conjugative position 
    std::sort(vec1.begin(), vec1.end());
    std::vector<int>::iterator vec2;
    // Overriding the duplicate element
    vec2 = std::unique(vec1.begin(), vec1.end());
    vec1.erase(vec2, vec1.end());
      std ::cout<< " after deletion : ";
    for(int i=0; i < vec1.size(); i++)
        std::cout << vec1.at(i) << ' ';
    return 0;
}
Output :

after deletion : 0 1 2 3 4 5 6 7 8 9

Example 2 : Removing duplicate objects based on their name –

Let we have a class People  with name and id as member variables  and  we want to remove duplicate elements from a vector of class people objects based on name .

#include <iostream>
#include <algorithm>
#include <vector>
class People
{
public:
    std::string MN;
    int ID;
    People(std::string name, int id) :
            MN(name), ID(id) {
    }
    bool operator <(const People & obj)
    {
        if (ID < obj.ID)
            return true;
        else
            return false;
    }
    bool operator ==(const People & obj) 
    {
        if (ID == obj.ID)
            return true;
        else
            return false;
    }
    
};
struct peopleequalcomp
{
    bool operator()(const People & fst, const People & snd)
    {
        if (fst.MN == snd.MN)
            return true;
        else
            return false;
    }
};
struct peoplecomp 
{
    bool operator()(const People & fst, const People & snd)
    {
        if (fst.MN < snd.MN)
            return true;
        else
            return false;
    }
};
 
int main()
{
   std::vector<People> vp = { People("AAA", 1), People("BBB", 2),
            People("BBB", 3),People("AAA", 4) };
    std::sort(vp.begin(), vp.end(), peoplecomp());
    std::vector<People>::iterator NP;
    NP = std::unique(vp.begin(),vp.end(), peopleequalcomp()); 
    
    vp.erase( NP, vp.end());
    std::cout << "Removing duplicate based on Name ....\n";
    std::for_each(vp.begin(), vp.end(), [](People & obj) {
        std::cout<<obj.ID<< " : "<<obj.MN<<std::endl;
    });
}

Output :

Removing duplicate based on Name ....
1 : AAA
2 : BBB

Example 3 : Removing duplicate objects by lambda function –

Let we have a class People  with name and id as member variables  and  we want to remove duplicate elements from a vector of class people objects based on name  with lambda function .

#include <iostream>
#include <algorithm>
#include <vector>
class People
{
public:
    std::string MN;
    int ID;
    People(std::string name, int id) :
            MN(name), ID(id) {
    }
    bool operator <(const People & obj)
    {
        if (ID < obj.ID)
            return true;
        else
            return false;
    }
    bool operator ==(const People & obj) 
    {
        if (ID == obj.ID)
            return true;
        else
            return false;
    }
    
};
struct peopleequalcomp
{
    bool operator()(const People & fst, const People & snd)
    {
        if (fst.MN == snd.MN)
            return true;
        else
            return false;
    }
};
struct peoplecomp 
{
    bool operator()(const People & fst, const People & snd)
    {
        if (fst.MN < snd.MN)
            return true;
        else
            return false;
    }
};
 
int main()
{
   std::vector<People> vp = { People("AAA", 1), People("BBB", 2),
            People("BBB", 3),People("AAA", 1) ,People("DDD", 5),People("EEE", 2),People("CCC", 3) };
      std::sort(vp.begin(), vp.end(), [](const People & fst, const People & sec) {
        if (fst.MN < sec.MN)
            return true;
        else
            return false;
    });
    std::vector<People>::iterator NP;
    NP = std::unique(vp.begin(), vp.end(), [](const People & fst, const People & sec) {
        if (fst.MN == sec.MN)
            return true;
        else
            return false;
    }); 
     vp.erase( NP, vp.end());
    std::cout << "Removing duplicate Persons  based on Name ......\n";
    std::for_each(vp.begin(), vp.end(), [](People & obj) {
        std::cout<<obj.ID<< " :: "<<obj.MN<<std::endl;
    });
}
Output :

Removing duplicate Persons  based on Name ......
1 :: AAA
2 :: BBB
3 :: CCC
5 :: DDD
2 :: EEE

Example 4 : Removing duplicate objects based on there ID –

Let we have a class People  with name and id as member variables  and  we want to remove duplicate elements from a vector of class people objects based on ID .

#include <iostream>
#include <algorithm>
#include <vector>
class People
{
public:
    std::string MN;
    int ID;
    People(std::string name, int id) :
            MN(name), ID(id) {
    }
    bool operator <(const People & obj)
    {
        if (ID < obj.ID)
            return true;
        else
            return false;
    }
    bool operator ==(const People & obj) 
    {
        if (ID == obj.ID)
            return true;
        else
            return false;
    }
    
};
 

struct peopleequalcomp
{
    bool operator()(const People & fst, const People & snd)
    {
        if (fst.MN == snd.MN)
            return true;
        else
            return false;
    }
};
struct peoplecomp 
{
    bool operator()(const People & fst, const People & snd)
    {
        if (fst.MN < snd.MN)
            return true;
        else
            return false;
    }
};
 
int main()
{
   std::vector<People> vp = { People("AAA", 1), People("BBB", 2),
            People("BBB", 3),People("CCC", 1) ,People("DDD", 5),People("EEE", 2),People("FFF", 3) };
   std::sort(vp.begin(), vp.end());
    vp.erase(std::unique(vp.begin(), vp.end()) , vp.end());
    std::cout << "Removing duplicate Person based on ID ......\n";
    std::for_each(vp.begin(), vp.end(), [](People & obj) {
        std::cout<<obj.ID<< " : "<<obj.MN<<std::endl;
    });
}
Output :
Removing duplicate Person based on ID ......
1 : AAA
2 : BBB
3 : BBB
5 : DDD