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

Function Difference

arrayOperations.go:200–231  ·  view source on GitHub ↗

Difference returns a slice of values that are only present in one of the input slices example #1 a := []int{1, 2, 2, 4, 6} b := []int{2, 4, 5} fmt.Println(Difference[int](a, b)) output: []int{1, 5, 6} example #2 fmt.Println(Difference[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

198// fmt.Println(Difference[int]([]int{1, 1, 3, 4, 5, 6}))
199// // output: []int{1, 3, 4, 5, 6}
200func Difference[T comparable](arrs ...[]T) []T {
201 m := make(map[T]int)
202
203 var (
204 tmpArr []T
205 count int
206 ok bool
207 )
208 for idx1 := range arrs {
209 tmpArr = Distinct(arrs[idx1])
210
211 for idx2 := range tmpArr {
212 count, ok = m[tmpArr[idx2]]
213 if !ok {
214 m[tmpArr[idx2]] = 1
215 } else {
216 m[tmpArr[idx2]] = count + 1
217 }
218 }
219 }
220
221 var (
222 ret []T
223 )
224 for k, v := range m {
225 if v == 1 {
226 ret = append(ret, k)
227 }
228 }
229
230 return ret
231}

Callers 1

TestDifferenceFunction · 0.85

Calls 1

DistinctFunction · 0.85

Tested by 1

TestDifferenceFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…