# How to create an array consists of indices of elements in one array that exists in the other array?

**URL:** <https://discourse.julialang.org/t/how-to-create-an-array-consists-of-indices-of-elements-in-one-array-that-exists-in-the-other-array/38237>\
**Category:** New to Julia\
**Created:** [April 26, 2020, 1:11pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-consists-of-indices-of-elements-in-one-array-that-exists-in-the-other-array/38237 "2020-04-26T13:11:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 26, 2020, 1:11pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-consists-of-indices-of-elements-in-one-array-that-exists-in-the-other-array/38237/1 "2020-04-26T13:11:55Z")

</div>

I firstly declared 2 arrays:

```julia
coord1 = [[0.0, 0.0],
          [1.0, 0.0],
          [0.0, 1.0],
          [1.0, 1.0],
          [0.0, 2.0],
          [1.0, 2.0]]
 
 coord2 = [[1.0, 0.0],
            [2.0, 0.0],
            [1.0, 1.0],
            [2.0, 1.0]]

```

I would like to create

1. an array `index1` that consists of indice of elements in coord2 that exist in coord1
2. an array `index2` that consists of indice of elements in coord1 that exist in coord2

I create the below code :

```julia
index1 = []
index2 = []

for (index, value) in enumerate(coord2)
    if value in coord1
        a = findfirst(x->x==value, coord1)
        push!(index1, a)
        push!(index2, index)
    end
end

```

Is there any better way towards this problem?

---

<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:** [April 26, 2020, 2:06pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-consists-of-indices-of-elements-in-one-array-that-exists-in-the-other-array/38237/2 "2020-04-26T14:06:17Z")

</div>

First, if this is an array of 2-component coordinate vectors, I would strongly recommend using an array of [`StaticArrays`](https://github.com/JuliaArrays/StaticArrays.jl) instead, e.g.

```julia
using StaticArrays
coord2 = [SVector(1.0,0.0), SVector(2.0,0.0), SVector(1.0,1.0), SVector(2.0,1.0)]

```

which will be vastly more efficient for virtually any computation you might want to perform.

What algorithm you might want to use here depends on how long your `coord1` and `coord2` arrays are and whether you care about efficiency. The double loop in your implementation has complexity \Theta(N\_1 N\_2) if your arrays have lengths N\_1 and N\_2, respectively. If you want something faster for large arrays (or if you just want shorter code) you could, for example, use a dictionary mapping coordinates to indices:

```julia
dict1 = Dict(c => i for (i, c) in enumerate(coord1))
dict2 = Dict(c => i for (i, c) in enumerate(coord2))
index1 = [dict2[c] for c in coord1 if haskey(dict2, c)]
index2 = [dict1[c] for c in coord2 if haskey(dict1, c)]

```

---

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 27, 2020, 7:37am UTC](https://discourse.julialang.org/t/how-to-create-an-array-consists-of-indices-of-elements-in-one-array-that-exists-in-the-other-array/38237/3 "2020-04-27T07:37:49Z")

</div>

Thank you a lot for your reply! I haven’t known StaticArray before.  
I learned new technique to combine dictionary and array.  
Just one more question: How to know that which is faster code?  
Is it to use `@time` macro ?

---

<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:** [April 27, 2020, 12:14pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-consists-of-indices-of-elements-in-one-array-that-exists-in-the-other-array/38237/4 "2020-04-27T12:14:18Z")

</div>

> [@QUANG\_VU\_NGUY\_N\_PH\_M](#):
>
> Just one more question: How to know that which is faster code?  
> Is it to use `@time` macro ?

The `@time` macro is fine for things that take a really long time. For high-resolution timing of smaller tasks, use the BenchmarkTools package. You have to be especially careful (use `$` interpolation, as explained in the Benchmark tools manual) when benchmarking code that uses global variables, in order to avoid dynamic-dispatch overhead that occurs in global scope.

---

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 28, 2020, 11:21pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-consists-of-indices-of-elements-in-one-array-that-exists-in-the-other-array/38237/5 "2020-04-28T23:21:45Z")

</div>

Thank you a lot!
