1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 03:02:28 +08:00

refactor: refact Intersection function

This commit is contained in:
dudaodong
2022-07-18 15:33:45 +08:00
parent fe6495123c
commit 1782b11ec4

View File

@@ -653,14 +653,20 @@ func Intersection[T comparable](slices ...[]T) []T {
var res []T var res []T
reducer := func(s1, s2 []T) []T { reducer := func(sliceA, sliceB []T) []T {
s := make([]T, 0, 0) hashMap := make(map[T]int)
for _, v := range s1 { for _, val := range sliceA {
if Contain(s2, v) { hashMap[val] = 1
s = append(s, v) }
out := make([]T, 0)
for _, val := range sliceB {
if v, ok := hashMap[val]; v == 1 && ok {
out = append(out, val)
hashMap[val]++
} }
} }
return s return out
} }
res = reducer(slices[0], slices[1]) res = reducer(slices[0], slices[1])
@@ -672,7 +678,7 @@ func Intersection[T comparable](slices ...[]T) []T {
res = reducer(reduceSlice[0], reduceSlice[1]) res = reducer(reduceSlice[0], reduceSlice[1])
} }
return Unique(res) return res
} }
// SymmetricDifference oppoiste operation of intersection function // SymmetricDifference oppoiste operation of intersection function