Where std::unique can be used?

1. Remove all the duplicate elements from a container:

Many of you must have searched for std::unique with a view that it will remove all the duplicate elements from the container, and now you might feel a bit disappointed to know that it removes only the consecutive duplicate elements. But, although, std::unique cannot do so as per its definition, but applying a bit of logic, we can make that happen. What we need to do is just sort the array before applying std::unique, such that all equal elements become consecutive, and now we have std::unique to remove all the duplicate consecutive elements. So, std::unique can also be used to remove all the duplicate elements from a container.

Below is the implementation of the above method:

C++




// C++ program to demonstrate the use of std::unique
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> v = { 1, 2, 3, 3, 3, 10, 1, 2, 3, 7, 7, 8 };
 
    vector<int>::iterator ip;
 
    // Sorting the array
    std::sort(v.begin(), v.end());
    // Now v becomes 1 1 2 2 3 3 3 3 7 7 8 10
 
    // Using std::unique
    ip = std::unique(v.begin(), v.begin() + 12);
    // Now v becomes {1 2 3 7 8 10 * * * * * *}
    // * means undefined
 
    // Resizing the vector so as to remove the undefined
    // terms
    v.resize(std::distance(v.begin(), ip));
 
    // Displaying the vector after applying std::unique
    for (ip = v.begin(); ip != v.end(); ++ip) {
        cout << *ip << " ";
    }
 
    return 0;
}


Output

1 2 3 7 8 10 

Explanation:

Firstly, we sorted the array such that all the equal duplicate elements become consecutive and now applying std::unique to it such that the duplicacy is removed, and in this way we remove all the duplicate elements from a container, whether consecutive or not.

Count unique elements: It can also be used if we want to count the total no. of unique elements in a container.

C++




// C++ program to demonstrate the use of std::unique
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
    vector<int> v = { 1, 1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8 };
 
    vector<int>::iterator ip;
 
    int count;
    sort(v.begin(), v.end());
 
    // Using std::unique and std::distance to count
    // unique elements in a container
    count = std::distance(
        v.begin(), std::unique(v.begin(), v.begin() + 12));
 
    // Displaying the value of count
    cout << "Total no. of unique elements = " << count;
 
    return 0;
}


Output

Total no. of unique elements = 5

Explanation:

As we know that std::unique returns an iterator to what should be the new end of the container after removing duplicate elements, so just counting the total no. of elements from beginning till this new end with the help of std::distance, should give us the total no. of unique elements in the container.



std::unique in C++

In C++, std::unique is used to remove duplicates of any element present consecutively in a range[first, last). It performs this task for all the sub-groups present in the range having the same element present consecutively.

  • It does not delete all the duplicate elements, but it removes duplicacy by just replacing those elements with the next element present in the sequence which is not duplicate to the current element being replaced. All the elements which are replaced are left in an unspecified state.
  • Another interesting feature of this function is that it does not change the size of the container after deleting the elements, it just returns a pointer pointing to the new end of the container, and based on that we have to resize the container or remove the garbage elements.

Time Complexity: O(N)
Space Complexity: O(1)

Similar Reads

Usage of std::unique in C++

The std::unique can be used in two ways as shown below:...

Where std::unique can be used?

...