-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
implement unique! #20549
Comments
I don't understand by |
julia> x = [2, 2, 3, 1, 2, 3, 1];
julia> unique(x) # Retrieves unique elements without modifying x
3-element Array{Int64,1}:
2
3
1
julia> unique!(x) # Modifies x to only contain its unique elements in order of occurrence
3-element Array{Int64,1}:
2
3
1
julia> x # Proof that x has been modified
3-element Array{Int64,1}:
2
3
1 |
I would like to implement this.
|
That's a start, but notice that One possible approach (probably not the best) would be to locate the indices in the input which contain duplicated elements, then just call |
How about we sort the given iterable in place and remove the duplicates ?
|
The problem with sorting is that you lose the order of occurrence. Notice in my example above that A very naive way to do it, posting here solely for the sake of example, would be function unique!(itr)
seen = Set{eltype(itr)}()
for (i, x) in enumerate(itr)
if x in seen
# If x has already been encountered in itr, remove it
deleteat!(itr, i)
else
# Otherwise note that we've encountered it
push!(seen, x)
end
end
return itr
end Does that approach make sense? This is somewhat similar to how |
The implementation above will give julia> A = [1, 2, 1];
julia> unique!(A)
ERROR: BoundsError: attempt to access 2-element Array{Int64,1} at index [4]
in next at ./iterator.jl:48 [inlined]
in unique!(::Array{Int64,1}) at ./REPL[23]:3 I think the indices of the duplicates should be stored somehow and then the
|
@fredrikekre to avoid |
Feel free to open a work in progress PR, then its easier for people to comment on your implementation :) |
We have a
unique
function that produces a new collection similar to its argument, but with each item only occurring once (in order of first appearance). There should be a correspondingunique!
function that removes recurrences of items and returns this modified collection. Note that for performance, at least on dense arrays, resizing of the array should only occur at end when you know the final size to shrink the array to. Part of #20402.The text was updated successfully, but these errors were encountered: