Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

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}
bool[typeof(x[0])] y;

foreach (e ; x)
    y[e] = true;

D doesn't have a set type, use an associative array.
You may replace typeof(x[0]) with the named type of the elements.
import std.container;
auto y = redBlackTree(x);
(def y (set x))

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