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: Adding the equivalent of checking if two slices are the same #498

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
22 changes: 22 additions & 0 deletions intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ func EveryBy[T any](collection []T, predicate func(item T) bool) bool {
return true
}

// Equivalent Returns true if the subset has the same elements and the same number of each element as the collection.
func Equivalent[T comparable](collection, subset []T) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following #498 (comment)

Suggested change
func Equivalent[T comparable](collection, subset []T) bool {
func SameItems[T comparable](collection, subset []T) bool {

Maybe this name would be more appropriate

l := len(collection)
if l != len(subset) {
return false
}

var m = make(map[T]int, l)
for i := range collection {
m[collection[i]] += 1
}
for i := range subset {
m[subset[i]] -= 1
}
for _, v := range m {
if v != 0 {
return false
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your logic requires extra space in the map and also 3 times iterating over the map.
Why not just sort the 2 arrays and compare if both are the same ??

What are your thoughts ??

Also after adding the function, you can add the function to Readme also.
Look this PR as reference : #494

Copy link
Author

@Dokiys Dokiys Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because just != or == is allowed on generics comparable, we can't sort this slice.
Additionally, sort.Slice() will change the parameter's original order, which I think is unsafe.

I have added doc and removed an unessential iterating based on your suggestion.

return true
}

// Some returns true if at least 1 element of a subset is contained into a collection.
// If the subset is empty Some returns false.
func Some[T comparable](collection []T, subset []T) bool {
Expand Down
23 changes: 23 additions & 0 deletions intersect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ func TestEveryBy(t *testing.T) {
is.True(result4)
}

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

result1 := Equivalent([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5})
is.True(result1)

result2 := Equivalent([]int{2, 3, 4, 5, 0, 1}, []int{0, 1, 2, 3, 4, 5})
is.True(result2)

result3 := Equivalent([]int{0, 1, 1, 3, 0, 0}, []int{0, 1, 3, 0, 1, 0})
is.True(result3)

result4 := Equivalent([]int{0, 1, 2, 3, 4}, []int{0, 1, 2, 3, 4, 5})
is.False(result4)

result5 := Equivalent([]int{0, 1, 2, 3, 4, 6}, []int{0, 1, 2, 3, 4, 5})
is.False(result5)

result6 := Equivalent([]int{0, 1, 1, 1, 1, 1}, []int{0, 0, 1, 1, 1, 1})
is.False(result6)
}

func TestSome(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down