# How to find the index of the two largest values in a 1D array

**URL:** <https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 24, 2023, 4:04pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341 "2023-05-24T16:04:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [May 24, 2023, 4:04pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/1 "2023-05-24T16:04:27Z")

</div>

Hi, I’m trying to find the index of the two largest values in an array.  
I’ve been trying to work through [find n smallest values in an n dims array](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092/9) but am getting confused because that is a 3D array.

This probably has a simple solution but it is evading me. I can’t use `findmax()` or `argmax()` since I don’t just want the largest value.

```julia
M = rand(6)

```

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [May 24, 2023, 4:09pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/2 "2023-05-24T16:09:38Z")

</div>

How about something like this:

```julia
julia> a = rand(6)
6-element Vector{Float64}:
 0.1911141673445511
 0.27806173115685107
 0.37965238565322046
 0.8664939772144525
 0.5317414946847623
 0.2850934557396858

julia> sortperm(a, rev=true)[1:2]
2-element Vector{Int64}:
 4
 5

```

Edit: @DNF’s suggestion is to use `partialsortperm` … which is for sure better in this case. @fatteneder’s answer is apparently faster, and allocates less.

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [May 24, 2023, 4:13pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/3 "2023-05-24T16:13:39Z")

</div>

EDIT: Updated to return the indices of the two largest values and not their values.

* * *

If linear complexity is desired then just use a loop, because they are also fast in Julia:

```julia
function findlargest2(a::AbstractVector)
    @assert length(a) >= 2
    i1, i2 = 1, 2
    max1, max2 = a[i1], a[i2]
    if max1 < max2
        max1, max2 = max2, max1
    end
    for i = 3:length(a)
        ai = a[i]
        if ai > max2
            if ai > max1
                i1, i2 = i, i1
                max1, max2 = ai, max1
            else
                i2 = i
                max2 = ai
            end
        end
    end
    return i1, i2
end

```

```julia
julia> x = randn(8)
8-element Vector{Float64}:
  0.8934664691033656
 -1.5294590308293632
 -0.13047453971043912
  1.8016634206952808
 -0.1771167975283893
  2.097991657439888
 -1.1881023747938395
 -0.04860363743772466

julia> findlargest2(x)
(6, 4)

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [May 24, 2023, 4:17pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/4 "2023-05-24T16:17:06Z")

</div>

It works, so thank you!. Why is `rev = true` (reverse sorting is performed)? If I make it false, the answer doesn’t make sense. Say the answer with `rev = true` is 4,2. I would expect the answer when `rev = false` to be 2,4 but the answer is 6, 5.

---

<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:** [May 24, 2023, 5:23pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/5 "2023-05-24T17:23:12Z")

</div>

> [@frylock](#):
>
> `sortperm(a, rev=true)[1:2]`

This will sort the entire array, which is a lot of unnecessary work. Instead, take a look at `partialsortperm`.

---

<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:** [May 24, 2023, 5:25pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/6 "2023-05-24T17:25:41Z")

</div>

> [@HelgavonLichtenstein](#):
>
> Say the answer with `rev = true` is 4,2. I would expect the answer when `rev = false` to be 2,4 but the answer is 6, 5.

`rev` refers to the sorting order. If `rev=false` you will get the two smallest instead of the largest.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 24, 2023, 8:31pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/7 "2023-05-24T20:31:25Z")

</div>

using argmax(.)

```julia

M = rand(6)

i1=argmax(M)

M[i1]=-Inf

i2=argmax(M)

```

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [May 25, 2023, 7:43pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/8 "2023-05-25T19:43:02Z")

</div>

> [@fatteneder](#):
>
> ```julia
> for i = 1:length(a)-2
> 
> ```

I believe that should be

```julia
    for i = 3:length(a)

```

But otherwise that is a very effective approach — non-allocating and significantly faster than `partialsortperm`.

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [May 25, 2023, 7:51pm UTC](https://discourse.julialang.org/t/how-to-find-the-index-of-the-two-largest-values-in-a-1d-array/99341/9 "2023-05-25T19:51:23Z")

</div>

Indeed, thanks for spotting. I update my version above!
