Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
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