# How best to maintain collection of indices into 2-d array

**URL:** https://discourse.julialang.org/t/how-best-to-maintain-collection-of-indices-into-2-d-array/29921
**Category:** New to Julia
**Created:** [October 15, 2019, 3:53am UTC](https://discourse.julialang.org/t/how-best-to-maintain-collection-of-indices-into-2-d-array/29921 "2019-10-15T03:53:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mpf01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpf01/32/8221_2.png) [@mpf01](https://discourse.julialang.org/u/mpf01)
#### Post date: [October 15, 2019, 3:53am UTC](https://discourse.julialang.org/t/how-best-to-maintain-collection-of-indices-into-2-d-array/29921/1 "2019-10-15T03:53:36Z")

</div>

I’d like to maintain a collection of index pairs (i,j) into a 2-dimensional array. The list must not contain any repeated elements, and elements can be added and deleted. `Set` seems to do the trick:

```julia
A = Set([(1,1),(3,3)])
union!(A, [(2,2),(3,3)]) # add only new element (2,2)
setdiff!(A, [(3,3)]) # remove item

B = rand(3,3)
let t = 0
    for ind in A
        i,j = ind
        t += B[i,j]
    end
end

```

Is this the best way to do this sort of thing in Julia?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 15, 2019, 4:50am UTC](https://discourse.julialang.org/t/how-best-to-maintain-collection-of-indices-into-2-d-array/29921/2 "2019-10-15T04:50:43Z")

</div>

Probably yes. If you need other features of the collection (eg ordering, fallback defaults), check out

> **[GitHub - JuliaCollections/DataStructures.jl: Julia implementation of Data...](https://github.com/JuliaCollections/DataStructures.jl)**
>
> Julia implementation of Data structures. Contribute to JuliaCollections/DataStructures.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [October 15, 2019, 12:50pm UTC](https://discourse.julialang.org/t/how-best-to-maintain-collection-of-indices-into-2-d-array/29921/3 "2019-10-15T12:50:39Z")

</div>

Depending on the sparsity and what you’re using the indices for, it may be more efficient to use a `SparseMatrix{Bool}` than a `Set`.

```julia
using SparseArrays
A = sparse([1, 3], [1, 3], ones(Bool, 2))
A[2,2] |= true # add element
A[3,3] &= false # remove element

B = rand(3,3)
t = sum(B .* A) # will have a better memory access pattern than the above for-loops

```

---

<div class="post-metadata">

### Author: ![mpf01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpf01/32/8221_2.png) [@mpf01](https://discourse.julialang.org/u/mpf01)
#### Post date: [October 16, 2019, 12:09am UTC](https://discourse.julialang.org/t/how-best-to-maintain-collection-of-indices-into-2-d-array/29921/4 "2019-10-16T00:09:17Z")

</div>

Thanks for this suggestion. Your reply also pointed me to `|=` and `&=`, which are new to me.
