I have a vector containing few non-adjacent duplicates.
As a simple example, consider:
2 1 6 1 4 6 2 1 1
I am trying to make this vector
unique by removing the non-adjacent duplicates and maintaining the order of elements.
Result would be:
2 1 6 4
The solutions I tried are:
- Inserting into a std::set but the problem with this approach is that it will disturb the order of elements.
- Use the combination of std::sort and std::unique. But again same order problem.
Manual duplicate elimination:
Define a temporary vector TempVector. for (each element in a vector) { if (the element does not exists in TempVector) { add to TempVector; } } swap orginial vector with TempVector.
My question is:
Is there any STL algorithm which can remove the non-adjacent duplicates from the vector ? what is its complexity?
See Question&Answers more detail:os