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

Idiom #118 List to set

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

Illustration
#include <set>
std::set<T> y (x.begin (), x.end ());
#include<unordered_set>
std::unordered_set<T> y (x.begin (), x.end ());
bool[typeof(x[0])] y;

foreach (e ; x)
    y[e] = true;
import std.container;
auto y = redBlackTree(x);
y = x |> Enum.uniq |> List.to_tuple
y = MapSet.new(x)
1
y := make(map[T]struct{}, len(x))
for _, v := range x {
	y[v] = struct{}{}
}
import Data.Set as Set
y = Set.fromList x
var y = new Set(x);
import java.util.HashSet;
import java.util.List;
Set<T> y = new HashSet<>(x);
local hash = {}
local y = {}
for _,v in ipairs(x) do
   if (not hash[v]) then
       y[#y+1] = v
       hash[v] = true
   end
end
my %y = map {$_=>0} @x;
y = set(x)
require 'set'
y = x.to_set
use std::collections::HashSet;
let y = x.iter().cloned().collect::<HashSet<_>>();

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms