MergeStringSlices use sets

Signed-off-by: helen <haitao.zhang@daocloud.io>
This commit is contained in:
helen 2023-06-09 14:47:00 +08:00
parent f92e576f6b
commit e89d7204eb

View File

@ -16,7 +16,11 @@
package util
import "strings"
import (
"strings"
"k8s.io/apimachinery/pkg/util/sets"
)
// InStringSlice checks whether a string is inside a string slice.
// Comparison is case insensitive.
@ -44,16 +48,7 @@ func SubtractStringSlice(ss []string, str string) []string {
// MergeStringSlices merges 2 string slices into one and remove duplicated elements.
func MergeStringSlices(a []string, b []string) []string {
set := map[string]struct{}{}
for _, s := range a {
set[s] = struct{}{}
}
for _, s := range b {
set[s] = struct{}{}
}
var ss []string
for s := range set {
ss = append(ss, s)
}
return ss
set := sets.NewString(a...)
set.Insert(b...)
return set.UnsortedList()
}