Skip to content

Commit 1f0d3f4

Browse files
committed
fix: omit duplicates in second Intersect list
1 parent 43cef1f commit 1f0d3f4

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

intersect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func Intersect[T comparable, Slice ~[]T](list1, list2 Slice) Slice {
111111
for i := range list2 {
112112
if _, ok := seen[list2[i]]; ok {
113113
result = append(result, list2[i])
114+
delete(seen, list2[i])
114115
}
115116
}
116117

intersect_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ func TestIntersect(t *testing.T) {
181181
result3 := Intersect([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
182182
result4 := Intersect([]int{0, 6}, []int{0, 1, 2, 3, 4, 5})
183183
result5 := Intersect([]int{0, 6, 0}, []int{0, 1, 2, 3, 4, 5})
184+
result6 := Intersect([]int{0, 1, 2, 3, 4, 5}, []int{0, 6, 0})
184185

185186
is.Equal([]int{0, 2}, result1)
186187
is.Equal([]int{0}, result2)
187188
is.Empty(result3)
188189
is.Equal([]int{0}, result4)
189190
is.Equal([]int{0}, result5)
191+
is.Equal([]int{0}, result6)
190192

191193
type myStrings []string
192194
allStrings := myStrings{"", "foo", "bar"}

0 commit comments

Comments
 (0)