How to find unique values into Vector{Vector{Int64}}?

Hi
I’m trying to get the unique values into A variable bellow, but unique(A) isn’t showing the correct result, is repeating A. How could I get the real result: [1,2,4,5,6,7,8,10,11] ?

``````A
5-element Vector{Vector{Int64}}:
[1, 2, 4]
[5, 6, 7, 8, 9, 11]
[5, 6, 7, 8, 9, 10, 11]
[5, 6, 7, 8, 9, 10]
[5, 6, 7, 8, 9]

unique(A)
5-element Vector{Vector{Int64}}:
[1, 2, 4]
[5, 6, 7, 8, 9, 11]
[5, 6, 7, 8, 9, 10, 11]
[5, 6, 7, 8, 9, 10]
[5, 6, 7, 8, 9]
``````
``````unique(Iterators.flatten(A))
``````

is a bit more verbose and a bit more efficient.

4 Likes

I always forget about `flatten`…thank you for that!

2 Likes

Your answer was fine too, it shows the splat operator which is forgotten often too. I think it doesn’t deserve to be deleted

3 Likes

The answers are spot on. I just wanted to add what was going on in the original version:

`unique(A)` does show the “correct” result, since it finds all unique elements in a collection. From the outermost layer, there is a vector `A` and all its elements are indeed different, so `unique(A) == A`.

2 Likes

What was the answer with splat?
`unique(vcat(A...))`

?

It was, you can see when clicking on the pen symbol on the post.

Most people would probably recommend `reduce(vcat, A)` instead of the splat.

1 Like