Skip to content

Commit

Permalink
feat: implementing the same function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohit Vachhani authored and Mohit Vachhani committed Apr 17, 2024
1 parent 71d8341 commit a93376b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,13 @@ func WithoutEmpty[T comparable](collection []T) []T {

return result
}

// Same returns boolean, whether the two collections are same or not.
func Same[T comparable](list1 []T, list2 []T) bool {
if len(list1) != len(list2) {
return false
}

left, right := Difference(list1, list2)
return len(left) == 0 && len(right) == 0
}
24 changes: 24 additions & 0 deletions intersect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,27 @@ func TestWithoutEmpty(t *testing.T) {
is.Equal(result2, []int{1, 2})
is.Equal(result3, []int{})
}

func TestSame(t *testing.T) {
t.Parallel()
is := assert.New(t)

arr1 := []int{0, 1, 2, 3, 4, 5}
arr2 := []int{0, 1, 2, 3, 4}
arr3 := []int{2, 1, 5, 3, 4, 0}
arr4 := []int{1, 2, 3, 4, 5, 1}
arr5 := []int{1, 2, 3, 4, 1, 5}
arr6 := []int{1, 2, 3, 4, 5}

isSameResult1 := Same(arr1, arr2)
is.Equal(isSameResult1, false)

isSameResult2 := Same(arr1, arr3)
is.Equal(isSameResult2, true)

isSameResult3 := Same(arr4, arr5)
is.Equal(isSameResult3, true)

isSameResult4 := Same(arr4, arr6)
is.Equal(isSameResult4, false)
}

0 comments on commit a93376b

Please sign in to comment.