-
Notifications
You must be signed in to change notification settings - Fork 5
/
unique.go
70 lines (55 loc) · 929 Bytes
/
unique.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package slice
func UniqueInt64(s []int64) []int64 {
size := len(s)
if size == 0 {
return []int64{}
}
m := make(map[int64]struct{})
for i := 0; i < size; i++ {
m[s[i]] = struct{}{}
}
realLen := len(m)
ret := make([]int64, realLen)
idx := 0
for key := range m {
ret[idx] = key
idx++
}
return ret
}
func UniqueInt(s []int) []int {
size := len(s)
if size == 0 {
return []int{}
}
m := make(map[int]struct{})
for i := 0; i < size; i++ {
m[s[i]] = struct{}{}
}
realLen := len(m)
ret := make([]int, realLen)
idx := 0
for key := range m {
ret[idx] = key
idx++
}
return ret
}
func UniqueString(s []string) []string {
size := len(s)
if size == 0 {
return []string{}
}
m := make(map[string]struct{})
for i := 0; i < size; i++ {
m[s[i]] = struct{}{}
}
realLen := len(m)
ret := make([]string, realLen)
idx := 0
for key := range m {
ret[idx] = key
idx++
}
return ret
}