Logo

Programming-Idioms

Declare and initialize a set x containing unique objects of type T.
New 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
#include <unordered_set>
std::unordered_set<T, hasher, eq> x;
#include <unordered_set>
std::unordered_set<T> x;
using System.Collections.Generic;
HashSet<T> x = new HashSet<T>();
import std.container: redBlackTree;
auto x = redBlackTree!T;
var x = new Set<T>();
x := make(map[T]struct{})
x := make(map[T]bool)
import Data.Set
x = empty :: Set T
let x = new Set();
import java.util.Set;
import java.util.HashSet;
Set<T> x = new HashSet<T>();
x.add(a);
x.add(b);
import java.util.Set;
import java.util.HashSet;
Set<T> x = new HashSet<T>();
import static java.util.Set.of;
import java.util.Set;
Set<T> x = of(a, b, c);
use Moops;
use Set::Object qw();
class T {}
class Set::Object::T extends Set::Object {
    method BUILDARGS(T @items) { return {data => \@items}; }
    method insert(T @items) { $self->next::method(@items); }
}
my $x = Set::Object::T->new(T->new, T->new, T->new);
class T(object):
    pass

x = set(T())
class T:
   ...

s = set(T() for _ in range(x))
class T:
    def __init__(self, x):
        self.x = x
    def __hash__(self):
        return hash(self.x)
    def __eq__(self, t):
        return self.x == t.x
x = {T('abc'), T(123), T(lambda: ...)}
require 'set'
x = Set.new
use std::collections::HashSet;
let x: HashSet<T> = HashSet::new();
val x = Set[T]()