- Notifications
You must be signed in to change notification settings - Fork1k
chore: remove duplicates using the symmetric difference function#14469
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -62,6 +62,20 @@ func Overlap[T comparable](a []T, b []T) bool { | ||
}) | ||
} | ||
func UniqueFunc[T any](a []T, equal func(a, b T) bool) []T { | ||
cpy := make([]T, 0, len(a)) | ||
for _, v := range a { | ||
if ContainsCompare(cpy, v, equal) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Why the repeated iteration? Can you not just keep a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I am trying to avoid the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Gotcha. We probably want to be careful that we don't use this for too large a slice then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yea, atm the slices are in the order of 5-10 elements | ||
continue | ||
} | ||
cpy = append(cpy, v) | ||
} | ||
return cpy | ||
} | ||
// Unique returns a new slice with all duplicate elements removed. | ||
func Unique[T comparable](a []T) []T { | ||
cpy := make([]T, 0, len(a)) | ||
@@ -109,15 +123,15 @@ func Descending[T constraints.Ordered](a, b T) int { | ||
} | ||
// SymmetricDifference returns the elements that need to be added and removed | ||
// to get from set 'a' to set 'b'. Note that duplicates are ignored in sets. | ||
// In classical set theory notation, SymmetricDifference returns | ||
// all elements of {add} and {remove} together. It is more useful to | ||
// return them as their own slices. | ||
// Notation: A Δ B = (A\B) ∪ (B\A) | ||
// Example: | ||
// | ||
//a := []int{1, 3, 4} | ||
//b := []int{1, 2, 2, 2} | ||
//add, remove := SymmetricDifference(a, b) | ||
//fmt.Println(add) // [2] | ||
//fmt.Println(remove) // [3, 4] | ||
@@ -127,6 +141,8 @@ func SymmetricDifference[T comparable](a, b []T) (add []T, remove []T) { | ||
} | ||
func SymmetricDifferenceFunc[T any](a, b []T, equal func(a, b T) bool) (add []T, remove []T) { | ||
// Ignore all duplicates | ||
a, b = UniqueFunc(a, equal), UniqueFunc(b, equal) | ||
return DifferenceFunc(b, a, equal), DifferenceFunc(a, b, equal) | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.