# How to find pairs of indices that meet specific criteria

**URL:** <https://discourse.julialang.org/t/how-to-find-pairs-of-indices-that-meet-specific-criteria/77119>\
**Category:** Performance\
**Tags:** question\
**Created:** [February 26, 2022, 12:22pm UTC](https://discourse.julialang.org/t/how-to-find-pairs-of-indices-that-meet-specific-criteria/77119 "2022-02-26T12:22:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shouto](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@Shouto](https://discourse.julialang.org/u/Shouto)\
**Post date:** [February 26, 2022, 12:22pm UTC](https://discourse.julialang.org/t/how-to-find-pairs-of-indices-that-meet-specific-criteria/77119/1 "2022-02-26T12:22:52Z")

</div>

Hi all,

Let E be an array such that E = [3,5,3,5,3] so E[1] = 3,…,E[5] = 3.  
I want to find and collect the pairs of two indices such that (E[1],E[2]), (E[1],E[4]),(E[2],E[4]), (E[2],E[5]), (E[3],E[4]), (E[4], E[5]). That is, the couples (i,j) such that i \< j and E[i]≠E[j].

Is there a way to obtain it? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 26, 2022, 12:56pm UTC](https://discourse.julialang.org/t/how-to-find-pairs-of-indices-that-meet-specific-criteria/77119/2 "2022-02-26T12:56:47Z")

</div>

Yes.

```julia
julia> [(i, j) for i = eachindex(E) for j = i:lastindex(E) if E[i] != E[j]]
6-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (1, 4)
 (2, 3)
 (2, 5)
 (3, 4)
 (4, 5)

```

---

<div class="post-metadata">

**Author:** ![Shouto](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@Shouto](https://discourse.julialang.org/u/Shouto)\
**Post date:** [February 26, 2022, 1:22pm UTC](https://discourse.julialang.org/t/how-to-find-pairs-of-indices-that-meet-specific-criteria/77119/3 "2022-02-26T13:22:10Z")

</div>

thanks a lot! This works for me.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 26, 2022, 11:18pm UTC](https://discourse.julialang.org/t/how-to-find-pairs-of-indices-that-meet-specific-criteria/77119/4 "2022-02-26T23:18:59Z")

</div>

I think that, maybe, the most generic solution is this one:

```julia
julia> [(i, j) for i = eachindex(E) for j = Iterators.rest(E, i) if E[i] != j]
6-element Array{Tuple{Int64,Int64},1}:
 (2, 5)
 (2, 3)
 (2, 5)
 (2, 3)
 (4, 5)
 (4, 3)

```

Note that both solutions I gave start with `j` with the same value as `E[i]` but this is not a problem because `E[i] != E[i]` is never true (in a sane code).

EDIT NOTE: this code was simply wrong before, as I thought `Iterators.rest` returned indexes, not values.
