// Created on savesnippets.com ยท https://savesnippets.com/Q3emZkOfcLq1db package main import "fmt" type Set[T comparable] map[T]struct{} func New[T comparable](items ...T) Set[T] { s := make(Set[T], len(items)) for _, v := range items { s[v] = struct{}{} } return s } func (s Set[T]) Add(v T) { s[v] = struct{}{} } func (s Set[T]) Delete(v T) { delete(s, v) } func (s Set[T]) Has(v T) bool { _, ok := s[v]; return ok } func (s Set[T]) Size() int { return len(s) } func (s Set[T]) Slice() []T { out := make([]T, 0, len(s)) for k := range s { out = append(out, k) } return out } func main() { s := New("a", "b", "c", "a") fmt.Println(s.Size()) // 3 fmt.Println(s.Has("b")) // true s.Add("d") s.Delete("a") fmt.Println(s.Slice()) // [b c d] (order varies) }