From f5967688a0abd733f0b9db8282b0ba3a615bf9e2 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Mon, 2 Jun 2025 20:14:22 +0100 Subject: [PATCH] set: introduce set union Change-Id: mksqzkvmmntrpwzyzvlznxtlmqttzkuo Signed-off-by: oppiliappan --- set.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/set.go b/set.go index 2b0fe8d..943361a 100644 --- a/set.go +++ b/set.go @@ -1,5 +1,9 @@ package hashset +import ( + "maps" +) + type Set[T comparable] map[T]struct{} // New creates and returns a new empty Set. @@ -21,3 +25,11 @@ func (s Set[T]) Remove(value T) { func (s Set[T]) Size() int { return len(s) } + +// Union returns a new set containing all elements from s and other. +func (s Set[T]) Union(other Set[T]) Set[T] { + result := make(Set[T], len(s)+len(other)) + maps.Copy(result, s) + maps.Copy(result, other) + return result +} -- 2.43.0