list unique( ) in C++ STL

C++Server Side ProgrammingProgramming

Given is the task to show the functionality list unique( ) function in C++ in STL.

What is List in STL

List are containers that allow constant time insertion and deletion anywhere in sequence. List are implemented as doubly linked lists. List allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is unique( )

The list unique( ) is used to remove all the duplicate element in list.

Syntax

list_name.unique(binarypredicate name)

Syntax for binary predicate

Bool name(data_type a, data_type b)

Parameter

This function accepts a single parameter which is a binary predicate that returns true if the elements should be treated as equal.

Example

Input List − 2 2 6 7 9 9 9 10 5 5 

Output New List − 2 5 6 7 9 10

Input List − 3.14 5.56 7.62 9.00 0.45 7.62 9.00 7.62 0.45 3.00

Output New List − 0.45 3.00 3.14 5.56 7.62 9.00

Approach can be followed

  • First we create binary predicate function.

  • Then we initialize the list.

  • Then we define unique( ) function.

  • Then we print the list after unique operation.

By using the above approach we can remove the duplicate element from the list.

Example

/ / C++ code to demonstrate the working of list unique( ) function in STL
#include <iostream.h>
#include<list.h>
Using namespace std;
/ / function for binary predicate
Bool cmp(int a, int b){
   Return (abs(a) == abs(b))
}
int main ( ){
   List<int> list = { 13, 14, 13, 19, 20, 19, 15, 19, 20, 15, 15 };
   / / print the list
   cout<< “ Elements in List: “;
   for( auto x = List.begin( ); x != List.end( ); ++x)
      cout<< *x << “ “;
   / / declaring unique( ) function
   list.unique(cmp);
   / / printing new list after unique operation
   cout<< “List after unique operation: “;
   for( x=list.begin( ); x != list.end( ); ++x)
      cout<< “ “<<*x;
   return 0;
}

Output

If we run the above code then it will generate the following output

Input - Element in List : 13 14 13 19 20 19 15 19 20 15
Output - List after unique operation : 13 14 15 19 20

Example

/ / C++ code to demonstrate the working of list unique( ) function in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
/ / function for binary predicate
Bool cmp(float a, float b){
   Return (abs(a) == abs(b))
}
int main ( ){
   List <float>t; list = { 3.14, 5.56, 7.62, 9.00, 0.45, 7.62, 9.00, 7.62, 0.45, 3.00 };
   / / print the list
   cout<< “ Elements in List: “;
   for( auto x = List.begin( ); x != List.end( ); ++x)
      cout<< *x << “ “;
   / / declaring unique( ) function
   list.unique(cmp);
   / / printing new list after unique operation
   cout<< “List after unique operation: ”;
   for( x=list.begin( ); x != list.end( ); ++x)
      cout<< “ “<<*x;
   return 0;
}

Output

If we run the above code then it will generate the following output

Input - Element in List: 3.14 5.56 7.62 9.00 0.45 7.62 9.00 7.62 0.45 3.00
Output - List after unique operation: 0.45 3.00 3.14 5.56 7.62 9.00
raja
Published on 26-Feb-2020 10:51:30
Advertisements