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
proposal: sort: add the Dedup function #41517
Comments
This should probably be named 'Uniq' for the posix uniq command, and we had best wait until we get generics in Go. Then this will be trivial to implement. |
Thanks for the proposal. It's not obvious to me that Also, https://golang.org/doc/faq#x_in_std. This can be written outside of the standard library, so is it a common enough operation that it ought to be in the standard library? Can you point to some examples of code that would use this if it were available? I also tend to agree with @beoran that this ought to wait for generics. Thanks again. |
I'm not convinced this should be tied up with sorting at all. I expected that this would be slices.Uniq once generics happen. |
For my use cases, I've found it's far more common to start with unsorted data. Hence it may be faster to deduplicate via a map since sorting is O(n.log(n)) - provided the extra temporary memory use is acceptable. Providing this functionality via the Waiting for generics seems preferable. |
I think that finding duplicates is a part that should be presented in the language library, but implementation using generics is good. So I close it. Thanks for the comments! |
For the record, this is now achieved using |
There is a small suggestion for extending the sort package. There are tasks where you need to remove duplicates from a slice. Yes, on the one hand we can use the
map
. But the inconvenience is that you have to do this for each separate type in the program. We have a function for sorting a slice, searching by a slice. Why don't we have a slice deduplication feature?One could suggest doing by analogy with
sort.Slice
:or with
sort.Search
:Conceptually, the solution might look like this:
Solution inspired https://doc.rust-lang.org/std/vec/struct.Vec.html#method.dedup
The change may not affect the runtime of the language, be compatible with version 1. Can we consider this option?
If so, I will prepare a real CL.
cc @davecheney
The text was updated successfully, but these errors were encountered: