Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C++

Idiom #118 List to set

Create the set y from the list x.
x may contain duplicates. y is unordered and has no repeated values.

Turning the list [a,b,c,b] into the set {c,a,b}
#include <set>
std::set<T> y (x.begin (), x.end ());

Assuming that x is a list of T's and T has operator<. If T is hashable, it is better to use std::unordered_set.
#include<unordered_set>
std::unordered_set<T> y (x.begin (), x.end ());

Assuming that x is a list of T's and T is hashable.
(def y (set x))

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