/
slices.go
68 lines (62 loc) · 1.29 KB
/
slices.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
package utils
// Intersection of elements in s1 and s2
func Intersection(s1, s2 []string) (inter []string) {
hash := make(map[string]bool)
for _, e := range s1 {
hash[e] = false
}
for _, e := range s2 {
// If elements present in the hashmap then append intersection list.
if val, ok := hash[e]; ok {
if !val {
// only add once
inter = append(inter, e)
hash[e] = true
}
}
}
return inter
}
// Distinct returns elements in s1, not in s2
func Distinct(s1, s2 []string) (distinct []string) {
hash := make(map[string]bool)
for _, e := range s2 {
hash[e] = true
}
for _, e := range s1 {
if _, ok := hash[e]; !ok {
distinct = append(distinct, e)
}
}
return distinct
}
// Unique func Unique(s1 []string) (unique []string) returns unique elements in s1
func Unique(s1 []string) (unique []string) {
hash := make(map[string]bool)
for _, e := range s1 {
if _, ok := hash[e]; !ok {
unique = append(unique, e)
}
hash[e] = true
}
return unique
}
func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func IndexOf(s []string, e string) int {
for i, a := range s {
if a == e {
return i
}
}
return -1
}
func Remove(slice []interface{}, s int) []interface{} {
return append(slice[:s], slice[s+1:]...)
}