Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implementing the same function [Issue - 390] #440

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions intersect.go
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
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)
}