Related Articles
list unique() in C++ STL
• Last Updated : 04 Feb, 2021

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 using namespace std; // Function for binary_predicatebool compare(double a, double b){    return ((int)a == (int)b);} // Driver codeint main(){    list 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
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up