Say I have a list val list = List(34, 11, 98, 56, 43).
Now how do I find the index of the minimum element of the list (e.g. 1 in this case)?
Python's inbuilt function allows us to find it in one line, we can find the minimum in the list using the min() function and then use the index() function to find out the index of that minimum element.
To find the index of an element in a list, you use the index() function. It returns 3 as expected. However, if you attempt to find an element that doesn't exist in the list using the index() function, you'll get an error. To fix this issue, you need to use the in operator.
min() function in R Language is used to return the location of the first minimum value in the Numeric Vector.
On Scala 2.8:
List(34, 11, 98, 56, 43).zipWithIndex.min._2
I suppose the easiest way is list.indexOf(list.min)
. It will throw an exception when the list is empty, although so will Daniel's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With