diff --git a/pkg/utils/utils_v2.go b/pkg/utils/utils_v2.go index 029e70496..c2b5d7c10 100644 --- a/pkg/utils/utils_v2.go +++ b/pkg/utils/utils_v2.go @@ -89,6 +89,26 @@ func Contain[E comparable](es []E, e E) bool { return IndexOf(es, e) >= 0 } +// DuplicateAny judge whether it is repeated +func DuplicateAny[E any, K comparable](es []E, fn func(e E) K) bool { + t := make(map[K]struct{}) + for _, e := range es { + k := fn(e) + if _, ok := t[k]; ok { + return true + } + t[k] = struct{}{} + } + return false +} + +// Duplicate judge whether it is repeated +func Duplicate[E comparable](es []E) bool { + return DuplicateAny(es, func(e E) E { + return e + }) +} + // SliceToMapOkAny slice to map func SliceToMapOkAny[E any, K comparable, V any](es []E, fn func(e E) (K, V, bool)) map[K]V { kv := make(map[K]V)