# Get vector of vectors of indice of appearances for every unique element of an array

**URL:** <https://discourse.julialang.org/t/get-vector-of-vectors-of-indice-of-appearances-for-every-unique-element-of-an-array/87671>\
**Category:** Performance\
**Tags:** question, arrays\
**Created:** [September 22, 2022, 8:31pm UTC](https://discourse.julialang.org/t/get-vector-of-vectors-of-indice-of-appearances-for-every-unique-element-of-an-array/87671 "2022-09-22T20:31:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elii17](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@elii17](https://discourse.julialang.org/u/elii17)\
**Post date:** [September 22, 2022, 8:31pm UTC](https://discourse.julialang.org/t/get-vector-of-vectors-of-indice-of-appearances-for-every-unique-element-of-an-array/87671/1 "2022-09-22T20:31:28Z")

</div>

Hey, I am trying to get a vector of vectors containing the indices of the appearance of every unique element of an array. So far i have tried

> y = [1,2,2,3,5,5,6,1]
> 
> unique\_elements = unique(y)  
> unique\_elements = [1,2,3,5,6]

> index\_array = Vector{Vector}(undef,length(unique\_elements))
> 
> for (index,unique) in enumerate(unique\_elements)  
> all\_index = findall(x-\>x==unique,y)  
> index\_array[index] = all\_index  
> end

index\_array = [[1,8],[2,3],[4],[5,6],[7]]

Is there any way that is faster than this ? My problem is that the y array im actually working with has 3 000 000 elements and this therefore takes over a minute to do… So I was wondering if anyone had an idea on how to optimize this.  
Thanks !!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 22, 2022, 8:47pm UTC](https://discourse.julialang.org/t/get-vector-of-vectors-of-indice-of-appearances-for-every-unique-element-of-an-array/87671/2 "2022-09-22T20:47:14Z")

</div>

See: [Is there a function similar to numpy unique with inverse? - #6 by stevengj](https://discourse.julialang.org/t/is-there-a-function-similar-to-numpy-unique-with-inverse/80949/6)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 22, 2022, 8:50pm UTC](https://discourse.julialang.org/t/get-vector-of-vectors-of-indice-of-appearances-for-every-unique-element-of-an-array/87671/3 "2022-09-22T20:50:52Z")

</div>

> [@elii17](#):
>
> Is there any way that is faster than this ?

Your implementation is O(n^2) where n is the length of the array, so you can definitely do better by making a single pass over the array and using e.g. a dictionary (hash table) as in some of the posts I linked above.

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 22, 2022, 9:34pm UTC](https://discourse.julialang.org/t/get-vector-of-vectors-of-indice-of-appearances-for-every-unique-element-of-an-array/87671/4 "2022-09-22T21:34:09Z")

</div>

Is this a good function?

```julia
function dict_unique_indx(y)
    index_dict = Dict{eltype(y), Vector{Int64}}()

    for (indx, element) in pairs(y)
        if haskey(index_dict, element)
            push!(index_dict[element], indx)
        else
            index_dict[element] = [indx]
        end
    end
    index_dict    
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 22, 2022, 10:02pm UTC](https://discourse.julialang.org/t/get-vector-of-vectors-of-indice-of-appearances-for-every-unique-element-of-an-array/87671/5 "2022-09-22T22:02:13Z")

</div>

> [@qwerty](#):
>
> `index_dict = Dict{eltype(y), Vector{Int64}}()`

I would use `Int` rather than `Int64`, so it is the correct index type on 32-bit machines. Otherwise looks good.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 23, 2022, 5:35pm UTC](https://discourse.julialang.org/t/get-vector-of-vectors-of-indice-of-appearances-for-every-unique-element-of-an-array/87671/6 "2022-09-23T17:35:44Z")

</div>

just to rewrite the same function in another way

```julia
function dict_unique_indx(y)
    d = Dict{eltype(y), Vector{Int}}()
    for (i, e) in pairs(y)
       push!(get!(()->Vector{Int}(), d,e),i)
    end
    d    
end

```
