How to find max's index in a Seq, List or Array in F#

f#, functional-programming

Solution

I believe you are looking for something like:

let maxIndex seq = 
    fst (Seq.maxBy snd (Seq.mapi (fun i x -> i, x) seq))

Note that giving this function an empty sequence will result in an ArgumentException.

(Alternatively, written in pipelining style:

let maxIndex seq =  
    seq
    |> Seq.mapi (fun i x -> i, x)
    |> Seq.maxBy snd 
    |> fst

)

Problem

`Seq.max` finds the max number. I'd like to have something like `Seq.findIndex` `Seq.maxIndex` returns the index of the maximum element.

Original source

Related problems