EDIT based on information about the source data: The reason you see the insert installation completed faster than sorting the vector is because your input data is two ranges already sorted. For quicksort (commonly used by std::sort ), this is a degenerate case and one of the worst possible inputs you could give it. For input size 1e8 sorting changes from std::sort to std::stable_sort shorten the runtime from ~ 25 s to 9 s.
If you want to keep the original order of items, you can try something like the following, which stores a hash of all the elements. I have no idea what the effectiveness of this would be, but for example, you could use a hash and remove_if , as shown below:
struct Remover { explicit Remover(hash& found_items) : found_items_(found_items) { } bool operator()(const Iter& item) { retval = <does exist in hash>; add to hash; return retval; } hash& found_items_; }; hash dup_finder; Remover remover(dup_finder); std::erase(std::remove_if(src.begin(), src.end(), remover), src.end());
Original components of my answer:
If the items in the source container are already mostly sorted, you can see better performance with stable_sort rather than sorting before calling unique . I cannot guess without additional information about the yoru dataset, which could lead to option 3 working better than 1 & 2.
Option 3 should remove uniques, but keep in mind that, despite what you claim, it will still reorder the elements in the same way as the first two options.
source share