-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin.go
98 lines (87 loc) · 2.11 KB
/
in.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package in
// Convenience functions for golangs silly lack of set operations on slices.
// The lists to search in will get sorted as a side-effect
// This is fast for small slices, if you have a larger slice you are probably
// better off using a map.
// Strings checks existence of a string in a string slice
func Strings(haystack []string, needle string) bool {
for _, elem := range haystack {
if elem == needle {
return true
}
}
return false
}
// Ints checks existence of an int in an int slice
func Ints(haystack []int, needle int) bool {
for _, elem := range haystack {
if elem == needle {
return true
}
}
return false
}
// StringSubset checks if needles is a subset of haystack
func StringsSubset(haystack []string, needles []string) bool {
set := make(map[string]bool)
for _, value := range haystack {
set[value] = true
}
for _, value := range needles {
if _, found := set[value]; !found {
return false
}
}
return true
}
// IntSubset checks if needles is a subset of haystack
func IntsSubset(haystack []int, needles []int) bool {
set := make(map[int]bool)
for _, value := range haystack {
set[value] = true
}
for _, value := range needles {
if _, found := set[value]; !found {
return false
}
}
return true
}
//StringsIntersection checks whether there are elements common to haystack and needles
func StringsIntersection(s1 []string, s2 []string) bool {
var haystack, needles []string
if len(s1) >= len(s2) {
haystack, needles = s1, s2
} else {
haystack, needles = s2, s1
}
set := make(map[string]bool)
for _, value := range needles {
set[value] = true
}
for _, value := range haystack {
if set[value] {
return true
}
}
return false
}
//IntsIntersection checks whether there are elements common to haystack and needles
func IntsIntersection(s1 []int, s2 []int) bool {
var haystack, needles []int
if len(s1) >= len(s2) {
haystack, needles = s1, s2
} else {
haystack, needles = s2, s1
}
set := make(map[int]bool)
for _, value := range needles {
set[value] = true
}
for _, value := range haystack {
if set[value] {
return true
}
}
return false
}