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}
@import Foundation;
NSSet *y=[NSSet setWithArray:x];
(def y (set x))
#include<unordered_set>
std::unordered_set<T> y (x.begin (), x.end ());
#include <set>
std::set<T> y (x.begin (), x.end ());
using System.Collections.Generic;
var y = new HashSet<T>(x);
bool[typeof(x[0])] y;

foreach (e ; x)
    y[e] = true;
import std.container;
auto y = redBlackTree(x);
var y = x.toSet();
y = x |> Enum.uniq |> List.to_tuple
y = MapSet.new(x)
Y = sets:from_list(X).
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
}
y := make(map[T]struct{}, len(x))
for _, v := range x {
	y[v] = struct{}{}
}
import qualified Data.Set as Set
y = Set.fromList x
var y = new Set(x);
import static java.util.stream.Collectors.toSet;
import java.util.Set;
Set<T> y = x.stream().collect(toSet());
import java.util.HashSet;
import java.util.List;
Set<T> y = new HashSet<>(x);
import static java.util.Set.copyOf;
import java.util.Set;
Set<T> y = copyOf(x);
import java.util.Set;
import java.util.HashSet;
Set<T> y = new HashSet<T>(x);
import java.util.LinkedHashSet;
import java.util.Set;
Set<T> y = new LinkedHashSet<>(x);
(setf y (remove-duplicates 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
$y = array_unique($x);
for i := Low(X) to High(X) do Include(Y,X[i]);
my %y = map {$_=>0} @x;
use List::Util qw(uniq);
my @y = uniq @x;
y = set(x)
y = {*x}
require 'set'
y = x.to_set
use std::collections::HashSet;
let y: HashSet<_> = x.into_iter().collect();
val y = x.toSet
y := x asSet

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