best way to remove duplicates from a vector

 There are more then one way to convert a vector with duplicate elements to a vector with 

unique elements .

but here is best way .

we will use unique function and erase function together to acheive this goal .


code is :

   sort(result.begin() , result.end());

    result.erase(unique(result.begin() , result.end()), result.end());

i.e 

first sort the vector , then erase those elements which are duplicates consecutively .


references : 

https://practice.geeksforgeeks.org/problems/subsets-1587115621/1

https://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector

https://www.geeksforgeeks.org/vector-in-cpp-stl/

Comments