go slice切片差集
func main() {
leyangjun1 := []string{10,9,8}
leyangjun2 := []string{4,5,6,7,8}
retDiff := DifferenceSet(leyangjun1, leyangjun2)
fmt.Println(retDiff)
}
func DifferenceSet(a []uint32, b []uint32) []uint32 {
var c []uint32
temp := map[uint32]struct{}{}
for _, val := range b {
if _, ok := temp[val]; !ok {
temp[val] = struct{}{}
}
}
for _, val := range a {
if _, ok := temp[val]; !ok {
c = append(c, val)
}
}
return c
}
标签:slice,ok,temp,val,差集,DifferenceSet,Go,uint32
From: https://blog.51cto.com/u_16085147/6218172