|
@@ -19,6 +19,33 @@ func RemoveRepeatElement[T comparable](elements []T) []T {
|
|
|
return result
|
|
return result
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func RemoveRepeatElementByKey[T comparable, K comparable](elements []T, offerKeyFunc func(element T) K) []T {
|
|
|
|
|
+ if elements == nil || len(elements) == 0 {
|
|
|
|
|
+ return elements
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ result := make([]T, 0)
|
|
|
|
|
+ tempMap := make(map[any]interface{})
|
|
|
|
|
+ for _, element := range elements {
|
|
|
|
|
+ var key any
|
|
|
|
|
+
|
|
|
|
|
+ if offerKeyFunc == nil {
|
|
|
|
|
+ key = element
|
|
|
|
|
+ } else {
|
|
|
|
|
+ key = offerKeyFunc(element)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ l := len(tempMap)
|
|
|
|
|
+ tempMap[key] = nil
|
|
|
|
|
+
|
|
|
|
|
+ if len(tempMap) != l {
|
|
|
|
|
+ result = append(result, element)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return result
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func ToMap[S any, K comparable, V any](slice []S, fn func(sliceValue S) (K, V, error)) (map[K]V, error) {
|
|
func ToMap[S any, K comparable, V any](slice []S, fn func(sliceValue S) (K, V, error)) (map[K]V, error) {
|
|
|
m := make(map[K]V, len(slice))
|
|
m := make(map[K]V, len(slice))
|
|
|
for _, v := range slice {
|
|
for _, v := range slice {
|