C++ Standard Library C++ STL Library

C++ <list> - unique() Function



The C++ list::unique function is used to delete all consecutive duplicate elements from a list. Only the first element in each group of equal elements is left.

The elements are compared using operator== (in first version) or binary_pred (in second version). The elements removed are destroyed.

Syntax

//version 1
void unique();

//version 2
template <class BinaryPredicate>
  void unique (BinaryPredicate binary_pred);

Parameters

binary_pred Specify a binary function that accepts two element as arguments, and returns a value convertible to bool. It returns true to remove the element passed as first argument from the list, returns false otherwise.

Return Value

None.

Time Complexity

Linear i.e, Θ(n).

Example:

In the example below, the list::unique function is used to delete all consecutive duplicate elements from a list.

#include <iostream>
#include <list>
using namespace std;
 
int main (){
  list<int> MyList{30, 20, 50, 50, 40, 10, 10, 20, 60};
  list<int>::iterator it;

  cout<<"MyList contains: \n";
  for(it = MyList.begin(); it != MyList.end(); it++)
    cout<<*it<<" ";

  //sorting and printing the list
  MyList.sort();
  cout<<"\n\nAfter sorting, MyList contains: \n";
  for(it = MyList.begin(); it != MyList.end(); it++)
    cout<<*it<<" ";  

  //remove all duplicates using first version
  MyList.unique();
  cout<<"\n\nAfter removing duplicates, MyList contains: \n";
  for(it = MyList.begin(); it != MyList.end(); it++)
    cout<<*it<<" ";  

  return 0;
}

The output of the above code will be:

MyList contains: 
30 20 50 50 40 10 10 20 60 

After sorting, MyList contains: 
10 10 20 20 30 40 50 50 60 

After removing duplicates, MyList contains: 
10 20 30 40 50 60 

Example:

The example below describes the usage of second version of list::unique function, where int value of elements are compared to find out the uniqueness of the element.

#include <iostream>
#include <list>
using namespace std;
 
//a binary predicate to compare int value of elements
bool int_comp (double x, double y)
{ return ( int(x)==int(y) ); }

int main (){
  list<double> MyList{2.5, 2.5, 2.3, 5.3, 4.4, 4.9, 1.4, 7.9, 8.3};
  list<double>::iterator it;

  cout<<"MyList contains: \n";
  for(it = MyList.begin(); it != MyList.end(); it++)
    cout<<*it<<" ";

  //sorting and printing the list
  MyList.sort();
  cout<<"\n\nAfter sorting, MyList contains: \n";
  for(it = MyList.begin(); it != MyList.end(); it++)
    cout<<*it<<" ";  

  //remove all duplicates using second version
  //int value of elements are compared
  MyList.unique(int_comp);
  cout<<"\n\nAfter removing duplicates, MyList contains: \n";
  for(it = MyList.begin(); it != MyList.end(); it++)
    cout<<*it<<" ";  

  return 0;
}

The output of the above code will be:

MyList contains: 
2.5 2.5 2.3 5.3 4.4 4.9 1.4 7.9 8.3 

After sorting, MyList contains: 
1.4 2.3 2.5 2.5 4.4 4.9 5.3 7.9 8.3 

After removing duplicates, MyList contains: 
1.4 2.3 4.4 5.3 7.9 8.3 

❮ C++ <list> Library

5