Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find index of element with minimum value?

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)?

like image 868
Surya Avatar asked May 21 '10 13:05

Surya


People also ask

How do you find the index of the minimum value in Python?

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.

How do you find the index of an 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.

How do you find the index of a minimum value in R?

min() function in R Language is used to return the location of the first minimum value in the Numeric Vector.


2 Answers

On Scala 2.8:

List(34, 11, 98, 56, 43).zipWithIndex.min._2
like image 109
Daniel C. Sobral Avatar answered Sep 23 '22 05:09

Daniel C. Sobral


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.

like image 23
James Cunningham Avatar answered Sep 23 '22 05:09

James Cunningham