Created
September 2, 2024 16:12
-
-
Save koonix/f97755976e39b64ae04f9664667928f6 to your computer and use it in GitHub Desktop.
Go unique value tester.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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