Skip to content

Instantly share code, notes, and snippets.

@koonix
Created September 2, 2024 16:12
Show Gist options
  • Save koonix/f97755976e39b64ae04f9664667928f6 to your computer and use it in GitHub Desktop.
Save koonix/f97755976e39b64ae04f9664667928f6 to your computer and use it in GitHub Desktop.
Go unique value tester.
package seen
type Seen[T comparable] struct {
Map map[T]struct{}
}
func New[T comparable](size int) Seen[T] {
return Seen[T]{
Map: make(map[T]struct{}, size),
}
}
func (s Seen[T]) Seen(v T) bool {
_, has := s.Map[v]
if has {
return true
}
s.Map[v] = struct{}{}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment