Logo

Programming-Idioms

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

Idiom #34 Create a set of objects

Declare and initialize a set x containing unique objects of type T.

x := make(map[T]struct{})

The struct{} type is space efficient because it occupies zero bytes in memory.
x := make(map[T]bool)

There is no built-in Set type, but you can create a Map with key type T and boolean value (which will be ignored).
#include <unordered_set>
std::unordered_set<T, hasher, eq> x;

Using a custom hasher and eq (equality checking function)

New implementation...