MCPcopy Index your code
hub / github.com/adam-hanna/arrayOperations / Union

Function Union

arrayOperations.go:167–187  ·  view source on GitHub ↗

Union returns a slice that contains the unique values of all the input slices example #1 a := []int{1, 2, 2, 4, 6} b := []int{2, 4, 5} fmt.Println(Union[int](a, b)) output: []int{1, 2, 4, 5, 6} example #2 fmt.Println(Union[int]([]int{1, 1, 3, 4, 5, 6})) output: []int{1, 3, 4, 5, 6}

(arrs ...[]T)

Source from the content-addressed store, hash-verified

165// fmt.Println(Union[int]([]int{1, 1, 3, 4, 5, 6}))
166// // output: []int{1, 3, 4, 5, 6}
167func Union[T comparable](arrs ...[]T) []T {
168 m := make(map[T]struct{})
169
170 var tmpArr []T
171 for idx := range arrs {
172 tmpArr = Distinct(arrs[idx])
173
174 for idx2 := range tmpArr {
175 m[tmpArr[idx2]] = struct{}{}
176 }
177 }
178
179 ret := make([]T, len(m))
180 i := 0
181 for k := range m {
182 ret[i] = k
183 i++
184 }
185
186 return ret
187}
188
189// Difference returns a slice of values that are only present in one of the input slices
190//

Callers 1

TestUnionFunction · 0.85

Calls 1

DistinctFunction · 0.85

Tested by 1

TestUnionFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…