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

unique() changes input order #3174

Closed
diegozea opened this issue May 22, 2013 · 7 comments
Closed

unique() changes input order #3174

diegozea opened this issue May 22, 2013 · 7 comments

Comments

@diegozea
Copy link
Contributor

julia> unique(collect("AABBCC"))
3-element Char Array:
 'A'
 'C'
 'B'

julia> unique(collect("ABC"))
3-element Char Array:
 'A'
 'C'
 'B'

julia> @which  unique(collect("AABBCC"))
unique(C) at set.jl:97

The actual definition of unique using Set and union! change the input order of the array.

@StefanKarpinski
Copy link
Sponsor Member

I'm unclear what this issue means. What's the problem here?

@diegozea
Copy link
Contributor Author

I'm not sure if this is an issue or not.
I expect that the order doesn't change with respect to the order of the first apparition of every element... Like in R:

> unique(c("A","B","C"))
[1] "A" "B" "C"
> unique(c("A","A","B","B","C","C"))
[1] "A" "B" "C"

I notice it because I was using unique in a context where the order is important.

@diegozea
Copy link
Contributor Author

In my particular case, I was doing something like:

enumerate(unique(collect("AABBCC")))

And waiting for

(1,'A')
(2,'B')
(3,'C')

Instead of

(1,'A')
(2,'C')
(3,'B')

@diegozea
Copy link
Contributor Author

In this case sort! ( like was mentioned in #1645 ) can not be used. Here the important order is the input (user defined) order an not the numeric or alphabetic order.

But #1845 can be very useful in this case.

For now I'm using ( #550 can be useful here for avoid the last line ):

function unique_order{T}(ab::Array{T,1})
  intset = IntSet(ab...)
  out = [ delete!(intset,elem,0) for elem in ab ]
  out[ out .!= 0 ]
end

@JeffBezanson
Copy link
Sponsor Member

I guess it might as well preserve order.

@diegozea
Copy link
Contributor Author

Thanks Jeff!!!

@pao
Copy link
Member

pao commented May 23, 2013

Note (for future archaeologists) that this now does not behave like MATLAB's unique. The MATLAB version uses a string argument 'stable' to get this behavior. http://www.mathworks.com/help/matlab/ref/unique.html

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

No branches or pull requests

4 participants