-
-
Notifications
You must be signed in to change notification settings - Fork 803
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
Add Duplicate & DuplicateBy #122
Conversation
…ion and not the return of iteratee.
Would definitely be great to have in lo. Perhaps it would be nicer if they were named |
That's a good point. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi there,
Duplicates
looks better than Duplicate
👍
But since this function is not exactly the opposite of Uniq
, I would name it FindDuplicates
or OnlyDuplicates
. WDYT ?
Also:
- is it a "search helper" (find.go) or a "slice helper" ?
- what about the opposite:
FindUniq
/OnlyUniq
?
Feel free to suggest other names.
slice.go
Outdated
func Duplicate[T comparable](collection []T) []T { | ||
isDupl := make(map[T]bool, len(collection)) | ||
|
||
for _, item := range collection { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do it in a single loop:
for _, item := range collection {
duplicated, ok := isDupl[item]
if !ok {
isDupl[item] = false
} else if !duplicated {
isDupl[item] = true
result = append(result, item)
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it but this will actually change the output's order.
For example, with the test input: 1, 2, 2, 1, 2, 3
The output with the 2 loops version would be: 1, 2
Whereas the output with the single loop would be: 2, 1
I Also thought that this way the code was more consistent with DuplicateBy
.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hell yeah !
i didn't realize order was not conserved
Good points! I'll rename Duplicate/DuplicateBy to FindDuplicates/FindDuplicatesBy and I'll create FindUniques/FindUniquesBy. |
Codecov Report
@@ Coverage Diff @@
## master #122 +/- ##
==========================================
+ Coverage 89.81% 90.34% +0.53%
==========================================
Files 13 13
Lines 1414 1492 +78
==========================================
+ Hits 1270 1348 +78
Misses 141 141
Partials 3 3
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
closes #121