# Finding Position of Element in an Array

**URL:** <https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 19, 2018, 4:25pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502 "2018-07-19T16:25:22Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [February 4, 2019, 5:30pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/21 "2019-02-04T17:30:30Z")

</div>

> [@lmiq](#):
>
> I tried to use findfirst

But you still didn’t read the documentation… this would work:

```julia
a = ["a", "b", "c", "d"]
findfirst(isequal("c"), a)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 4, 2019, 5:38pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/22 "2019-02-04T17:38:30Z")

</div>

Slightly neater and more legible than my reinvention of the `isequal` function …

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 4, 2019, 5:42pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/23 "2019-02-04T17:42:19Z")

</div>

Thank you.

But there is a difference between not reading the documentation, which is in the link below and does not mention “isequal” anywhere, and not knowing how to combine different functions to obtain the desired result.

[https://docs.julialang.org/en/v1/base/strings/#Base.findfirst-Tuple{AbstractString,AbstractString}](https://docs.julialang.org/en/v1/base/strings/#Base.findfirst-Tuple%7BAbstractString,AbstractString%7D)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 4, 2019, 6:12pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/24 "2019-02-04T18:12:01Z")

</div>

That’s the link to searching inside a string. You are searching in an array.

Here you go: [Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Base.findfirst-Tuple%7BFunction,Any%7D)

---

<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:** [February 4, 2019, 7:10pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/25 "2019-02-04T19:10:17Z")

</div>

As @DNF pointed out, you were looking at the docs for another method.

In general, it is better to use `?findfirst` at the REPL, that way you don’t miss the right method.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [February 4, 2019, 11:45pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/26 "2019-02-04T23:45:37Z")

</div>

@nilshg: `isequal` and `==` are not the same. You can read about the difference in the `isequal` doumentation. In this case you could also write `findfirst(==("c"), a)` to get the same result.

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [April 2, 2019, 6:56pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/27 "2019-04-02T18:56:05Z")

</div>

findall command does not work at all.

v=rand(Float64,3);  
C = findall(x-\>x==min(v), v)

```julia
julia> v=rand(3)
3-element Array{Float64,1}:
 0.09242010387818334
 0.0444175027161271
 0.32294884973200766

julia> C = findall(x->x==min(v), v)
ERROR: MethodError: no method matching min(::Array{Float64,1})

```

how can we find the minimal value position in Julia?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 2, 2019, 7:11pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/28 "2019-04-02T19:11:51Z")

</div>

Please try to read the error message. It tells you what’s wrong, it doesn’t have anything to do with `findall`, but with `min`. Use `minimum` instead.

If you only need the position of the first minimum (not all of them), you can use `findmin(v)` instead.

---

<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:** [April 3, 2019, 6:22am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/29 "2019-04-03T06:22:31Z")

</div>

> [@DNF](#):
>
> Use `minimum` instead.

I think that would call `minimum` for every comparison though, which may not be efficient. Something like

```julia
findall(isequal(minimum(v)), v)

```

could avoid that.

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [April 4, 2019, 2:05am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/30 "2019-04-04T02:05:47Z")

</div>

Thanks you @Tamas_Papp

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [April 4, 2019, 3:32am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/31 "2019-04-04T03:32:50Z")

</div>

> [@Aquaman](#):
>
> how can we find the minimal value position in Julia?

Since you said “_the_ minimal position”, maybe you could use `argmin`:

```julia
help?> argmin
search: argmin

  argmin(itr) -> Integer

  Return the index of the minimum element in a collection. If there are multiple minimal elements, then the first one will be returned.

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 4, 2019, 4:01am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/32 "2019-04-04T04:01:16Z")

</div>

just to cooperate with the discusion, some benchmarks:

```julia
v = rand(10000)

```

```julia
julia> @btime argmin(v)
  18.699 μs (0 allocations: 0 bytes)
443

```

```julia
julia> @btime findall(isequal(minimum(v)), v)
  32.300 μs (8 allocations: 320 bytes)
1-element Array{Int64,1}:
 443

```

```julia
julia> @btime findmin(v)
  18.699 μs (1 allocation: 32 bytes)
(7.175518611357568e-5, 443)

```

```julia
julia> @btime findall(x->x==minimum(v), v)
  239.807 ms (20006 allocations: 312.77 KiB)
1-element Array{Int64,1}:
 443

```

obviusly this applies with the max versions too, so is good to know who actually performs faster.

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [April 4, 2019, 8:48am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/33 "2019-04-04T08:48:26Z")

</div>

the local minimum or global minimum is also a question.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 4, 2019, 8:58am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/34 "2019-04-04T08:58:40Z")

</div>

All methods gives the global minimum xi that xi \<= xj for all xj in a vector x, but findmin and argmin throw the first global min they can find, for example:

```julia
x=[1,2,3,4,1]

julia> findmin([1,2,3,4,1])
(1, 1)
julia> argmin([1,2,3,4,1])
1

```

but:

```julia
julia> findall(isequal(minimum(x)),x)
2-element Array{Int64,1}:
 1
 5

```

---

<div class="post-metadata">

**Author:** ![kirui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirui/32/8585_2.png) [@kirui](https://discourse.julialang.org/u/kirui)\
**Post date:** [July 31, 2019, 4:11pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/35 "2019-07-31T16:11:11Z")

</div>

How can you avoid memory allocation by using findall in a loop?  
I have a loop which goes upto 100000 and each time it finds the index of numbers matching a certain criteria.  
This is what I am talking about:  
`for i = 1:100000; cr = max(0.0, 0.2*sqrt(i) - 20); ftt = findall(x .<= cr); end`

x is an array of floats.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 31, 2019, 5:49pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/36 "2019-07-31T17:49:43Z")

</div>

how is `cr` an array?..

maybe you can sort array and find first, since find needs to make comparison

---

<div class="post-metadata">

**Author:** ![kirui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirui/32/8585_2.png) [@kirui](https://discourse.julialang.org/u/kirui)\
**Post date:** [July 31, 2019, 5:57pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/37 "2019-07-31T17:57:37Z")

</div>

Sorry, I have just edited. x is an array of floats

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [July 31, 2019, 6:39pm UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/38 "2019-07-31T18:39:53Z")

</div>

Pass `findall` a predicate so it doesn’t need to allocate the temporary `x .<= cr` array, _i.e._ `findall(xi -> xi <= cr, x)`. This produced a ~3x speedup on my machine.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [October 9, 2019, 8:21am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/39 "2019-10-09T08:21:39Z")

</div>

Just to say that I wish in v2 we’d have a simpler API for this - most languages have something in the line of `indexof`, where you get the index or -1 if not found (so it would also be type stable). In Julia one is forced to do some weird gymnastics involving some form of `find*` and `Some`. It was quite difficult to understand how to use these (when the previous function was deprecated) and I almost never know how to do it without checking the docs – or rather just searching and copying my code. In my humble opinion, t’s not developer friendly.

(PS - off topic but related - OMG how I wish there would be another name for `occursin`. Oh the time I spent trying to remember the name of this function! “To occur” is really not a commonly used English verb and it’s really hard for a non-native English speaker to remember it. I honestly had to repeat it 100 times at some point, exasperated, to commit it to memory).

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 9, 2019, 8:47am UTC](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502/40 "2019-10-09T08:47:35Z")

</div>

> [@essenciary](#):
>
> Just to say that I wish in v2 we’d have a simpler API for this - most languages have something in the line of `indexof` , where you get the index or -1 if not found (so it would also be type stable). In Julia one is forced to do some weird gymnastics involving some form of `find*` and `Some` .

You only have to use `Some` if `nothing` is a valid index which is quite rare. Otherwise just `find` / `findall` away?

```julia
julia> a = [1,2,3,4,5];

julia> findall(isequal(3), a)
1-element Array{Int64,1}:
 3

julia> findfirst(isequal(3), a)
3

```

What would a simpler API be?

[Previous page](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502.md?page=1)

[Next page](https://discourse.julialang.org/t/finding-position-of-element-in-an-array/12502.md?page=3)
