-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.go
52 lines (47 loc) · 1.03 KB
/
array.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package util
import (
"reflect"
utilString "github.com/linuxkungfu/go-util/string"
)
func MergeArray[T any](first []T, second []T) []T {
first = append(first, second...)
return first
}
func MergeUniqueArray[T any](first []T, second []T) []T {
valueMap := map[any]bool{}
for _, value := range first {
valueMap[value] = true
}
for _, value := range second {
_, ok := valueMap[value]
if !ok {
first = append(first, value)
valueMap[value] = true
}
}
return first
}
func UniqueArray[T any](values []T) []T {
newArray := make([]T, 0)
tempMap := make(map[string][]T, len(values))
for _, v := range values {
tempArray, exist := tempMap[utilString.AnyToStr(v)]
if exist {
found := false
for _, e := range tempArray {
if reflect.DeepEqual(e, v) {
found = true
break
}
}
if !found {
tempMap[utilString.AnyToStr(v)] = append(tempArray, v)
newArray = append(newArray, v)
}
} else {
tempMap[utilString.AnyToStr(v)] = []T{v}
newArray = append(newArray, v)
}
}
return newArray
}