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

Add Duplicate & DuplicateBy #122

Merged
merged 5 commits into from
Jul 4, 2022
Merged

Conversation

CorentinClabaut
Copy link
Contributor

closes #121

@mixedCase
Copy link

Would definitely be great to have in lo. Perhaps it would be nicer if they were named Duplicates and DuplicatesBy since Duplicate (IMO) gives off the impression of either a function like lo.Fill or lo.Repeat rather than one that returns duplicates.

@CorentinClabaut
Copy link
Contributor Author

That's a good point.
I'm happy to make the change but before I'll wait on @samber input to be sure he is interested in the function being added.

Copy link
Owner

@samber samber left a 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.

README.md Outdated Show resolved Hide resolved
slice.go Outdated
func Duplicate[T comparable](collection []T) []T {
isDupl := make(map[T]bool, len(collection))

for _, item := range collection {
Copy link
Owner

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)
    }
}

Copy link
Contributor Author

@CorentinClabaut CorentinClabaut Jul 4, 2022

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?

Copy link
Owner

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

README.md Outdated Show resolved Hide resolved
@CorentinClabaut
Copy link
Contributor Author

Good points!

I'll rename Duplicate/DuplicateBy to FindDuplicates/FindDuplicatesBy and I'll create FindUniques/FindUniquesBy.
I'll also move these functions to find.go

@codecov-commenter
Copy link

Codecov Report

Merging #122 (d21f7b5) into master (1a0b4e6) will increase coverage by 0.53%.
The diff coverage is 100.00%.

@@            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              
Flag Coverage Δ
unittests 90.34% <100.00%> (+0.53%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
find.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1a0b4e6...d21f7b5. Read the comment docs.

@samber samber merged commit dcaaeb0 into samber:master Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Duplicate & DuplicateBy
4 participants