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 treated as equal.
It has following syntax:
bool name(data_type a, data_type a)
Return value: The function does not return anything.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
forward_list< int > list = { 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 2, 4, 4 };
cout << "List of elements before unique operation is: " ;
for ( auto it = list.begin(); it != list.end(); ++it)
cout << *it << " " ;
list.unique();
cout << "\nList of elements after unique operation is: " ;
for ( auto it = list.begin(); it != list.end(); ++it)
cout << *it << " " ;
return 0;
}
|
Output
List of elements before unique operation is: 1 1 1 1 2 2 2 2 3 3 3 2 4 4
List of elements after unique operation is: 1 2 3 2 4
Time Complexity: O(N), where N is the number of elements compared
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Jun, 2023
Like Article
Save Article