Merge pull request #8663 from helen-frank/feature/MergeSortedStringSlices

MergeStringSlices use sets
This commit is contained in:
Derek McGowan 2023-08-22 16:31:28 -07:00 committed by GitHub
commit 2bac6ffb79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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