Logo

Programming-Idioms

  • Haskell
  • Clojure

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}
(def y (set x))
import qualified Data.Set as Set
y = Set.fromList x
#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.

New implementation...