generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslice.go
40 lines (37 loc) · 935 Bytes
/
slice.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
package u
// UniqueStrings removes duplicate values from a string slice.
func UniqueStrings(input []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range input {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
// UniqueInts removes duplicate values from an int slice.
func UniqueInts(input []int) []int {
keys := make(map[int]bool)
list := []int{}
for _, entry := range input {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
// UniqueInterfaces removes duplicate values from an interface slice.
func UniqueInterfaces(input []interface{}) []interface{} {
keys := make(map[interface{}]bool)
list := []interface{}{}
for _, entry := range input {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}