-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathslice.go
More file actions
42 lines (39 loc) · 800 Bytes
/
Copy pathslice.go
File metadata and controls
42 lines (39 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package xmux
// a 减去b的差集
func Subtract[T comparable](a, b []T) []T {
// want []string{"aa"}
bm := make(map[T]struct{})
for _, v := range b {
bm[v] = struct{}{}
}
temp := make([]T, 0, len(a))
for _, v := range a {
if _, ok := bm[v]; !ok {
temp = append(temp, v)
}
}
return temp
}
func SubtractSliceMap[T comparable](a []T, b map[T]struct{}) []T {
// want []string{"aa"}
temp := make([]T, 0, len(a))
for _, v := range a {
if _, ok := b[v]; !ok {
temp = append(temp, v)
}
}
return temp
}
// 重复的K 有多个key 也只返回一个
func SliceExsit(s1, s2 []string) (string, bool) {
mm := make(map[string]struct{})
for _, v := range s2 {
mm[v] = struct{}{}
}
for _, v := range s1 {
if _, ok := mm[v]; ok {
return v, true
}
}
return "", false
}