Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain the index of the maximum element

Tags:

c#

linq

Given such a list:

        List<int> intList = new List<int>();
        intList.Add(5);
        intList.Add(10);
        intList.Add(15);
        intList.Add(46);

how do you obtain the index of the maximum element in the list? In this case, it's at index 3.

Edit: It's a shame that standard LINQ doesn't ship this functionalities.

like image 527
Graviton Avatar asked Sep 06 '25 03:09

Graviton


1 Answers

this way :

var maxIndex = foo.IndexOf(foo.Max());
like image 77
Sadegh Avatar answered Sep 07 '25 21:09

Sadegh