diff --git a/intersect.go b/intersect.go index 2df0e741..f5f56ee1 100644 --- a/intersect.go +++ b/intersect.go @@ -102,6 +102,7 @@ func Intersect[T comparable, Slice ~[]T](list1 Slice, list2 Slice) Slice { for i := range list2 { if _, ok := seen[list2[i]]; ok { result = append(result, list2[i]) + delete(seen, list2[i]) } } diff --git a/intersect_test.go b/intersect_test.go index 53911599..8ab27174 100644 --- a/intersect_test.go +++ b/intersect_test.go @@ -181,12 +181,14 @@ func TestIntersect(t *testing.T) { result3 := Intersect([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6}) result4 := Intersect([]int{0, 6}, []int{0, 1, 2, 3, 4, 5}) result5 := Intersect([]int{0, 6, 0}, []int{0, 1, 2, 3, 4, 5}) + result6 := Intersect([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 1}) is.Equal(result1, []int{0, 2}) is.Equal(result2, []int{0}) is.Equal(result3, []int{}) is.Equal(result4, []int{0}) is.Equal(result5, []int{0}) + is.Equal(result6, []int{0, 1}) type myStrings []string allStrings := myStrings{"", "foo", "bar"}