-1

In a List<float[]> containing arrays of varying sizes, I want to find the Index of that array whose length is maximum in the List.

I'm using the following LINQ statement:

 var index = list
   .Select((arr, ind) => new { arr, ind })
   .Where(x => x.arr.Length == list.Max(a => a.Length))
   .Select(x => x.ind)
   .ToList()
   .First();

This seems to work but the query is returning a list of elements whose Length is equal to list.Max(a => a.Length) where after I have to use .First() to get the first Maximum Index.

Is there any more efficient way to do this so that I directly get the index value rather than a List to avoid use of First?

EDIT: As the question was marked duplicate, I want to clarify that I do not want the object in return but the index of that object itself. I've gone through all the posts that my question can be a potential duplicate of and found that they are all returning the object of Max Length. Though the difference is very thin but I believe my question is not a duplicate.

EDIT: After running a few test and measuring the time taken by each LINQ Satements submitted,Below is the table with time taken (in milliseconds) by each method during my tests

Test Max Aggregate GroupBy GroupBy OrderByDescending
First 13.5946 1.8927 7.3801 7.1061
Second 12.3544 1.9245 7.4852 7.1755
Third 12.1003 1.8772 7.3531 6.6891

Its easy to understand that Aggregate method given by Dmitry Bychenko is working far efficiently than any other used methods in this situation. Tim Schmelter's GroupBy and OrderByDescending Answer is also doing the same job but taking more time than Aggregate. Furthermore I wanted to avoid using First. Hence, I feel the Aggregate method is an appropriate solution for my question.

  • _"I believe my question is not a duplicate"_ -- of course your question is a duplicate. Even absent any attempt to search for an answer, the odds that you are the first person in the 13 year history of Stack Overflow to ever want to know the _index_ of the item in a collection with the maximum value of a given property would be infinitesimal. And of course, with a search we easily find such examples. Since you feel the other duplicates didn't serve your need sufficiently, I've added two more than _directly_ address your concern about the _index_ of the item. – Peter Duniho Jun 24 '21 at 17:42
  • @PeterDuniho "_Even absent any attempt ...._" -- Easily said. Possibly I did not use proper terms while searching but its wrong to say that I did not tried properly to search my query. Even the thought that I never searched for the query before posting a question that too when it always notify to look for potential solutions would be cretinous. And unfortunately, I wasn't able to find the right solution while posting. Yes, the questions you've added are giving the answer to the query I raised. – Abhinav Pandey Jun 24 '21 at 18:31
  • _"the thought that I never searched for the query...would be cretinous."_ -- says you. For anyone who's actually spent any significant time reading and answering questions on the site, the fact is that all too often people post without _any_ effort to search at all, and your question certainly has all the hallmark signs of exactly that, [given how trivial it is to find the duplicates](https://stackoverflow.com/search?q=%5Blinq%5D+max+value+index). Calling someone a cretin just because they've merely _hinted_ that your behavior is just like that seen from all the other new users is foolish. – Peter Duniho Jun 24 '21 at 19:05
  • @PeterDuniho Although I did not called you a cretin nor do I had any intention about the same, you have my sincere apologies. But being pointed for not looking properly earlier (yep I admit I did not looked properly) even when I tried in best of my knowledge made me go out of the line. Sorry :-) – Abhinav Pandey Jun 24 '21 at 19:34

1 Answers1

0

You can try general aggregating method Aggregate instead of specific Max:

  var index = list
    .Select((array, index) => (array, index))
    .Aggregate((s, a) => s.array.Length > a.array.Length ? s : a)
    .index;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215