Skip to content

Commit f72bc6b

Browse files
committed
intersection implementation in slices module
1 parent 3c43f8b commit f72bc6b

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

go.sum

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
22
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
4-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
53
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
64
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
7-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
8-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
95
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
106
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11-
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
12-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
137
golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE=
148
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=

slices/slices.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,15 @@ func Grow[S ~[]E, E any](s S, n int) S {
256256
func Clip[S ~[]E, E any](s S) S {
257257
return s[:len(s):len(s)]
258258
}
259+
260+
// Intersection finds elements that are present in both slices, returning new slice without duplicates
261+
func Intersection[E comparable](s1, s2 []E) []E {
262+
var result []E
263+
for i := 0; i < len(s1); i++ {
264+
element := s1[i]
265+
if Contains(s2, element) && !Contains(result, element) {
266+
result = append(result, element)
267+
}
268+
}
269+
return result
270+
}

slices/slices_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,68 @@ func BenchmarkReplace(b *testing.B) {
767767
}
768768

769769
}
770+
771+
func TestIntersection(t *testing.T) {
772+
intTypeCases := []struct {
773+
name string
774+
s, v []int
775+
want []int
776+
}{
777+
{
778+
name: "empty first slice",
779+
s: []int{},
780+
v: []int{1, 4},
781+
want: []int{},
782+
},
783+
{
784+
name: "empty second slice",
785+
s: []int{1, 3},
786+
v: []int{},
787+
want: []int{},
788+
},
789+
{
790+
name: "duplicates int",
791+
s: []int{2, 4, 1},
792+
v: []int{1, 3, 1},
793+
want: []int{1},
794+
},
795+
{
796+
name: "regular use",
797+
s: []int{1, 2, 3},
798+
v: []int{3, 4, 5},
799+
want: []int{3},
800+
},
801+
}
802+
strTypeCases := []struct {
803+
name string
804+
s, v []string
805+
want []string
806+
}{
807+
{
808+
name: "duplicates string",
809+
s: []string{"a", "b", "c"},
810+
v: []string{"b", "b", "z"},
811+
want: []string{"b"},
812+
},
813+
{
814+
name: "contain substring",
815+
s: []string{"abc", "h", "i"},
816+
v: []string{"ab", "g", "z"},
817+
want: []string{},
818+
},
819+
}
820+
for _, test := range intTypeCases {
821+
t.Run(test.name, func(tt *testing.T) {
822+
if got := Intersection(test.s, test.v); !Equal(got, test.want) {
823+
tt.Errorf("Intersection(%v) = %v, want %v", test.s, got, test.want)
824+
}
825+
})
826+
}
827+
for _, test := range strTypeCases {
828+
t.Run(test.name, func(tt *testing.T) {
829+
if got := Intersection(test.s, test.v); !Equal(got, test.want) {
830+
tt.Errorf("Intersection(%v) = %v, want %v", test.s, got, test.want)
831+
}
832+
})
833+
}
834+
}

0 commit comments

Comments
 (0)