I did it wrong. I realized later you can iterate through the vector once and store in a map, if element is present or not. Iterate through the vector again and each time check if the element is present as a key in the map. First encounter, delete the key val pair from the map. So that the next time same element appears in the vector, it would not be found in the map, so remove the element from the vector and shift all the subsequent vector elements backwards.