2 parents 835a2e9 + a4b2e3b commit 50d8885Copy full SHA for 50d8885
2 files changed
sets.go
@@ -66,6 +66,22 @@ func SetMerge(a []string, b []string) []string {
66
return merged
67
}
68
69
+// SetIntersect will return all elements present in both a and b
70
+func SetIntersect(a []string, b []string) []string {
71
+ intersection := make([]string, 0)
72
+
73
+ for _, aVal := range a {
74
+ for _, bVal := range b {
75
+ if aVal == bVal {
76
+ intersection = append(intersection, aVal)
77
+ break
78
+ }
79
80
81
82
+ return intersection
83
+}
84
85
// SortByKeys returns a new ordered slice based on the keys ordering
86
func SortByKeys(keys []string, strs []string) []string {
87
resultLen := len(strs)
sets_test.go
@@ -98,6 +98,39 @@ func TestSetMerge(t *testing.T) {
98
99
100
101
+func TestSetIntersect(t *testing.T) {
102
+ t.Parallel()
103
104
+ tests := []struct {
105
+ A []string
106
+ B []string
107
+ C []string
108
+ }{
109
+ {
110
+ []string{"a", "b", "c"},
111
112
113
+ },
114
115
116
+ []string{"b", "c", "d"},
117
+ []string{"b", "c"},
118
119
120
121
+ []string{"d", "e", "f"},
122
+ []string{},
123
124
125
126
+ for i, test := range tests {
127
+ intersection := SetIntersect(test.A, test.B)
128
+ if !reflect.DeepEqual(test.C, intersection) {
129
+ t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.C, intersection)
130
131
132
133
134
func TestSortByKeys(t *testing.T) {
135
t.Parallel()
136
0 commit comments