Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C++
#include <algorithm>
#include <vector>
std::sort(x.begin(), x.end());
auto last = std::unique(x.begin(), x.end());
x.erase(last, x.end());

List x must be a STL container like a std::vector.
#include <string>
#include <unordered_set>
#include <vector>
std::vector<std::string> x = {"one", "two", "two", "one", "three"};
std::unordered_set<std::string> t;
for (auto e : x)
    t.insert(e);

Original order is lost.
t contains the new list of unique objects.
(distinct x)

New implementation...
< >
programming-idioms.org