golang字符串列表操作(求包含、交集、并集、差集)
func ContainsString(src []string, dest string) bool {
for _, item := range src {
if item == dest {
return true
}
}
return false
}
// DifferenceStrings 取前者src与后者dest两个字符串列表的差集
func DifferenceStrings(src []string, dest []string) []string {
res := make([]string, 0)
for _, item := range src {
if !ContainsString(dest, item) {
res = append(res, item)
}
}
return res
}
// IntersectionStrings 取两个字符串列表的交集
func IntersectionStrings(src []string, dest []string) []string {
res := make([]string, 0)
for _, item := range src {
if ContainsString(dest, item) {
res = append(res, item)
}
}
return res
}
// UnionString 取两个字符串列表的并集
func UnionStrings(src []string, dest []string) []string {
res := make([]string, 0)
res = append(res, src...)
for _, item := range dest {
if !ContainsString(res, item) {
res = append(res, item)
}
}
return res
}