-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection.go
71 lines (64 loc) · 1.6 KB
/
intersection.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
package venn
// Intersect combines two different input slices a1 and a2
// provides unique result
func IntersectString(a1, a2 []string) []string {
r := make([]string, 0, len(a1)+len(a2))
uniqueA2 := deDuplicateString(a2)
lookup := stringconvertToMap(a1)
for _, data := range uniqueA2 {
if _, ok := lookup[data]; ok {
r = append(r, data)
}
}
return r
}
// IntersectInt gives the list of common elements between two data set of int
// provides unique result
func IntersectInt(a1, a2 []int) []int {
r := make([]int, 0, len(a1)+len(a2))
uniqA2 := deDuplicateint(a2)
lookUp := intconvertToMap(a1)
for _, data := range uniqA2 {
if _, ok := lookUp[data]; ok {
r = append(r, data)
}
}
return r
}
// IntersectFloat64 gives list of common elements between two data set of
// float64 provides a unique result
func IntersectFloat64(a1, a2 []float64) []float64 {
r := make([]float64, 0, len(a1)+len(a2))
uniqA2 := deDuplicatefloat64(a2)
lookUp := floatconvertToMap(a1)
for _, data := range uniqA2 {
if _, ok := lookUp[data]; ok {
r = append(r, data)
}
}
return r
}
func stringconvertToMap(a1 []string) stringLookUp {
r := make(stringLookUp, len(a1))
uniqA1 := deDuplicateString(a1)
for _, data := range uniqA1 {
r[data] = struct{}{}
}
return r
}
func intconvertToMap(a1 []int) intLookUp {
r := make(intLookUp, len(a1))
uniqA1 := deDuplicateint(a1)
for _, data := range uniqA1 {
r[data] = true
}
return r
}
func floatconvertToMap(a1 []float64) floatLookUp {
r := make(floatLookUp, len(a1))
uniqA1 := deDuplicatefloat64(a1)
for _, data := range uniqA1 {
r[data] = true
}
return r
}