# Findfirst on sorted array

**URL:** https://discourse.julialang.org/t/findfirst-on-sorted-array/4928
**Category:** General Usage
**Tags:** question
**Created:** [July 18, 2017, 12:58pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928 "2017-07-18T12:58:39Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 18, 2017, 12:58pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/1 "2017-07-18T12:58:39Z")

</div>

I have a sorted array of numbers:

```julia
A = [1, 4, 10, 15, 18]

```

I want to determine the first index where a value, `v`, occurs in this array (or 0 if it does not occur), like `findfirst(A, v)`. But I want to exploit the fact that `A` is already sorted (binary search). Is this already implemented in Julia? I did not find anything on the docs for `findfirst`.

---

<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: [July 18, 2017, 1:04pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/2 "2017-07-18T13:04:57Z")

</div>

Try `searchsortedfirst`.

Also, please learn how to use the help system (`apropos("sorted")` would have worked), and make a minimal effort to find answers before posting.

---

<div class="post-metadata">

### Author: ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)
#### Post date: [July 19, 2017, 5:25pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/3 "2017-07-19T17:25:00Z")

</div>

Pointing out `apropos("sorted")` seems useful, but the rest of the answer seems unnecessarily hostile to me. It’s pretty important for growing languages to be welcoming to newcomers.

---

<div class="post-metadata">

### Author: ![smldis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smldis/32/1735_2.png) [@smldis](https://discourse.julialang.org/u/smldis)
#### Post date: [July 19, 2017, 7:16pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/4 "2017-07-19T19:16:15Z")

</div>

Woo never noticed the `apropos` function 😄 , for sure you are welcome @bramtayl .

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [July 19, 2017, 7:35pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/5 "2017-07-19T19:35:12Z")

</div>

> [@Tamas\_Papp](#):
>
> make a minimal effort to find answers before posting.

This is very important though I don’t think this is a case where this rule is violated.  
Reading doc string, simple search of doc and google should definitely be done, but it’s pretty hard to put together the correct search terms for either google or our doc to return the right result in this case. Combinations of sorted, find, first does not return the right function for each.

We could raise the requirement to include `apropos` though a much more user friendly interface must be provided for that. (Preferably an online or interactive interface, I don’t think people will try to type keywords into the REPL help mode especially when multiple keyword doesn’t work there)

---

<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: [July 19, 2017, 9:31pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/6 "2017-07-19T21:31:30Z")

</div>

In Matlab (and I think also Python) each docstring includes a "See also… " section. In Matlab, the “See also” function names are clickable links to more docstrings. I find that I am rarely more than a couple of “see also” clicks away from the function I’m looking for. It’s pretty useful.

Despite having heard about it a dozen times at least, I always forget about the `apropos` function, or its name.

---

<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: [July 19, 2017, 10:15pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/7 "2017-07-19T22:15:22Z")

</div>

There are also “See also”-s in Julia documentation (clickable to what is referred). Still not too many but it is increasing steadily. Help with this is appreciated.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [July 19, 2017, 10:22pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/8 "2017-07-19T22:22:24Z")

</div>

I also tend to forget `apropos`. What is the rationale for the name `apropos`? If I understood this maybe it would stick.

---

<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: [July 19, 2017, 10:24pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/9 "2017-07-19T22:24:49Z")

</div>

Perhaps useful links:

> **[apropos (Unix)](https://en.wikipedia.org/wiki/Apropos_(Unix))**
>
> In computing, apropos is a command to search the man page files in Unix and Unix-like operating systems. Apropos takes its name from the French "à propos" (Latin "ad prōpositum") which means about. It is particularly useful when searching for commands without knowing their exact names.
> Often a wrapper for the man -k command, the apropos command is used to search the "name" sections of all manual pages for the string/s (called keyword/s) specified. The output is a list of all manual pages contain...

> **[Apropos Definition & Meaning | Dictionary.com](https://www.dictionary.com/browse/apropos)**
>
> Apropos definition, fitting; at the right time; to the purpose; opportunely. See more.

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [July 19, 2017, 11:44pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/10 "2017-07-19T23:44:13Z")

</div>

One of the best things about Python is that every possible dumb question has been asked on Stack Overflow, so google [gives the right answer straight-away, on top of two exact-match SO posts](https://encrypted.google.com/search?hl=en&q=find%20element%20in%20sorted%20array#hl=en&q=python+find+element+in+sorted+array). Julia is [significantly worse](https://encrypted.google.com/search?hl=en&q=find%20element%20in%20sorted%20array#hl=en&q=julia+find+element+in+sorted+array) in my experience.

---

<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: [July 20, 2017, 5:31am UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/11 "2017-07-20T05:31:46Z")

</div>

This was not my intention — if my tone came across as hostile, I apologize.

The names for this family of functions could be better organized, see  
[https://github.com/JuliaLang/julia/issues/10593](https://github.com/JuliaLang/julia/issues/10593)

---

<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: [June 23, 2020, 1:00pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/12 "2020-06-23T13:00:13Z")

</div>

Concerning the `searchsortedfirst`, we must note that the function does not return a value which is by itself indicative of a non-match. I think we should add that to verify if the element is or not in the list, we should add a test afterwards:

```julia
x = [1, 2, 3, 5]
function my_searchsortedfirst(x,i)
  index = searchsortedfirst(x,i)
  if index > lastindex(x) || x[index] != i
    return 0
  else
    return index
  end
end

julia> my_searchsortedfirst(x,4)
0

```

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [June 23, 2020, 2:00pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/13 "2020-06-23T14:00:40Z")

</div>

To obtain a value that is indicative of a non-match, you can use `searchsorted`, instead of `searchsortedfirst`.

(P.S. I’m not sure that this minor detail deserved reviving a thread that was created years ago! I think it would have been better to create a new post, maybe linking back to this one.)

---

<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: [June 23, 2020, 2:17pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/14 "2020-06-23T14:17:00Z")

</div>

Well, I didn’t have any question, actually. However, while searching for a solution, I found this thread, probably indicated by the search engines. While reading it, I noticed that the original question was not completely answered and it would be useful to have it better explained.

(also the output of `searchsorted` still needs to be interpreted, and finding the first occurrence is faster than all occurrences).

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [June 23, 2020, 2:28pm UTC](https://discourse.julialang.org/t/findfirst-on-sorted-array/4928/15 "2020-06-23T14:28:32Z")

</div>

> [@lmiq](#):
>
> While reading it, I noticed that the original question was not completely answered and it would be useful to have it better explained.

Well, it depends on how you see it: the pointer to `searchsortedfirst` did not answer it completely, but the advice of looking on `apropos("sorted")` did. 😉

Regarding the output of `searchsorted`, I think that `1:0`, `3:2`, etc. are as easy to interpret as a “non-match” as `0`. (EDIT: to clarify this statement, you only need to check if the length of that output is zero, which is a very cheap operation.) And I’m not sure about efficiency, but I guess that finding the first and last occurrences of a specific value in a sorted collection (with binary search) is not much more expensive than finding only the first one.
