The Wayback Machine - https://web.archive.org/web/20240711102516/https://www.geeksforgeeks.org/list-unique-in-c-stl/
Open In App

list unique() in C++ STL

Last Updated : 13 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

list::unique() is an inbuilt function in C++ STL which removes all duplicate consecutive elements from the list. It works only on sorted list.

Syntax: 

list_name.unique(BinaryPredicate name)

Parameters:  The function accepts a single and optional parameter which is a binary predicate that returns true if the elements should be treated as equal. It has following syntax: 

bool name(data_type a, data_type b);

Return value: This function does not return anything.

Below is the implementation of above function: 

CPP




// C++ program to illustrate the
// unique() function
#include <bits/stdc++.h>
using namespace std;
 
// Function for binary_predicate
bool compare(double a, double b)
{
    return ((int)a == (int)b);
}
 
// Driver code
int main()
{
    list<double> list = { 2.55, 3.15, 4.16, 4.16,
                          4.77, 12.65, 12.65, 13.59 };
 
    cout << "List is: ";
       
    //sort the list
    list.sort();
 
    // unique operation on list with no parameters
    list.unique();
 
    // starts from the first element
    // of the list to the last
    for (auto it = list.begin(); it != list.end(); ++it)
        cout << *it << " ";
 
    // unique operation on list with parameter
    list.unique(compare);
 
    cout << "\nList is: ";
 
    // starts from the first element
    // of the list to the last
    for (auto it = list.begin(); it != list.end(); ++it)
        cout << *it << " ";
 
    return 0;
}


Output

List is: 2.55 3.15 4.16 4.77 12.65 13.59 
List is: 2.55 3.15 4.16 12.65 13.59 

Time Complexity – O(nlogn) due to sorting of list

Auxiliary Space: O(1)


Previous Article
Next Article

Similar Reads

forward_list::unique() in C++ STL
forward_list::unique() is an inbuilt function in C++ STL which removes all consecutive duplicate elements from the forward_list. It uses binary predicate for comparison. Syntax: forwardlist_name.unique(BinaryPredicate name)Parameters: The function accepts a single parameter which is a binary predicate that returns true if the elements should be tre
2 min read
Find all unique subsets of a given set using C++ STL
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets, using C++ STL. Note: Each subset should be sorted. Examples: Input: N = 3, arr[] = {2, 1, 2}Output:(), (1), (1 2), (1 2 2), (2), (2 2)Explanation: All possible subsets = (), (2), (1), (1, 2), (2), (2 2), (2 1), (2, 1, 2)After S
3 min read
Count number of unique Triangles using STL | Set 1 (Using set)
We are given n triangles along with length of their three sides as a,b,c. Now we need to count number of unique triangles out of these n given triangles. Two triangles are different from one another if they have at least one of the sides different.Example: Input: arr[] = {{1, 2, 2}, {4, 5, 6}, {4, 5, 6} Output: 2 Input: arr[] = {{4, 5, 6}, {6, 5, 4
3 min read
list::push_front() and list::push_back() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::push_front() push_front() function is used to push elements into a list from the front. The n
4 min read
list::pop_front() and list::pop_back() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::pop_front() pop_front() function is used to pop or remove elements from a list from the front
4 min read
list::front() and list::back() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::front() This function is used to reference the first element of the list container. This func
3 min read
list::empty() and list::size() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::empty()empty() function is used to check if the list container is empty or not. Syntax : list
3 min read
list::remove() and list::remove_if() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::remove() remove() function is used to remove all the values from the list that correspond to
3 min read
list::emplace_front() and list::emplace_back() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::emplace_front()This function is used to insert a new element into the list container, the new
3 min read
list::begin() and list::end() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::begin() begin() function is used to return an iterator pointing to the first element of the l
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg