Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: implementing the same function
  • Loading branch information
Mohit Vachhani authored and Mohit Vachhani committed Apr 17, 2024
commit a93376bf948daf2a4bbb64c9890d130e40c255d1
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)
}