Logo

Programming-Idioms

# 118 List to set
Create the set y from the list x.
x may contain duplicates. y is unordered and has no repeated values.
Implementation
Haskell

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Haskell implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import java.util.HashSet;
import java.util.List;
Set<T> y = new HashSet<>(x);
y := make(map[T]struct{}, len(x))
for _, v := range x {
	y[v] = struct{}{}
}
y = set(x)
bool[typeof(x[0])] y;

foreach (e ; x)
    y[e] = true;
import std.container;
auto y = redBlackTree(x);
var y = new Set(x);
require 'set'
y = x.to_set
my %y = map {$_=>0} @x;
y = x |> Enum.uniq |> List.to_tuple
y = MapSet.new(x)
use std::collections::HashSet;
let y: HashSet<_> = x.into_iter().collect();
local hash = {}
local y = {}
for _,v in ipairs(x) do
   if (not hash[v]) then
       y[#y+1] = v
       hash[v] = true
   end
end
#include<unordered_set>
std::unordered_set<T> y (x.begin (), x.end ());
#include <set>
std::set<T> y (x.begin (), x.end ());
$y = array_unique($x);
for i := Low(X) to High(X) do Include(Y,X[i]);
val y = x.toSet
var y = x.toSet();
using System.Collections.Generic;
var y = new HashSet<T>(x);
(setf y (remove-duplicates x))
(def y (set x))
Y = sets:from_list(X).
@import Foundation;
NSSet *y=[NSSet setWithArray:x];
use List::Util qw(uniq);
my @y = uniq @x;
import java.util.Set;
import java.util.HashSet;
Set<T> y = new HashSet<T>(x);
y := x asSet
func sliceToSet[T comparable](x []T) map[T]struct{} {
	y := make(map[T]struct{}, len(x))
	for _, v := range x {
		y[v] = struct{}{}
	}
	return y
}