How do I get the index of the highest value in an array using LINQ?

c# linq get object with max value
get max value from list c# linq
c# get max value from list
get max id from list in linq
get max value from list<int c#
get max date from list c# using linq
c# select max from list
find largest number in array c#

I have an array of doubles and I want the index of the highest value. These are the solutions that I've come up with so far but I think that there must be a more elegant solution. Ideas?

double[] score = new double[] { 12.2, 13.3, 5, 17.2, 2.2, 4.5 };
int topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).OrderByDescending(x => x.Item).Select(x => x.Index).First();

topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).OrderBy(x => x.Item).Select(x => x.Index).Last();

double maxVal = score.Max();
topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).Where(x => x.Item == maxVal).Select(x => x.Index).Single();

C# find highest array value and index, Linq namespace. It returns maximal value of numeric collection. Max for Numeric Types. Gets maximal number from list of integer numbers. var� This is the function for finding Highest number in Array using LINQ. public void FindMaxNumber() {int[] numbers = { 10,30,55,2,8,9,66,120,3,4 }; int maxNum = numbers.Min(); Console.WriteLine("The minimum number is {0}.", maxNum);} We can also use Max to get the length of the Longest word in an array like

Meh, why make it overcomplicated? This is the simplest way.

var indexAtMax = scores.ToList().IndexOf(scores.Max());

Yeah, you could make an extension method to use less memory, but unless you're dealing with huge arrays, you will never notice the difference.

LINQ Max, In LINQ, you can find the maximum element of the given sequence by using Max () value from the given array. using System;. using System.Linq;. class GFG {. Step 1: Assign array value. Assume largest number as array's first value and its index as 0. Step 2: Iterate array using a for loop. Step 3: Check max value is smaller than array current value, if true reassign the max value and its index position.

var scoreList = score.ToList();
int topIndex =
      from x
      in score
      orderby x
      select scoreList.IndexOf(x)

If score wasn't an array this wouldn't be half bad...

LINQ, (must have using System.Linq; ) int maxValue = anArray.Max(); int maxIndex = anArray.ToList(). Assume largest number as array's first value and its index as 0� Max (LINQ) Enumerable.Max is extension method from System.Linq namespace. It returns maximal value of numeric collection. Max for Numeric Types. Gets maximal number from list of integer numbers.

MoreLinq is a library that provides this functionality and much more.

C# find highest array value and index, It seems so easy with one line of code to pull out the highest number, so I Also the Linq extension Max has to iterate through your whole public int GetMaxArrayElement (int[] array, out int index) {; index = -1;; int max = int. How can I find the second maximum number in a list of values using Linq and C#? For example I have this list: List<double> ListOfNums = new List<double> {1, 5, 7, -1, 4, 8}; Is there a method I can get the second maximum value in the list which is 7?

I had this problem today (to get the index in a users array who had highest age), and I did on this way:

var position = users.TakeWhile(u => u.Age != users.Max(x=>x.Age)).Count();

It was on C# class, so its noob solution, I´am sure your ones are better :)

identifying element number in array, after finding max, Use Unity to build high-quality 3D and 2D games, deploy them across mobile, Linq.Enumerable.Iterate[Single,Single] (IEnumerable`1 source, Single initValue, System.Func`3 Min is required to return any value but if there's no value at all it can't return anything. Array index out of range error 1 Answer. The fastest way to build a Max formula that finds the highest value in a range is this: In a cell, type =MAX (Select a range of numbers using the mouse. Type the closing parenthesis.

Getting index of the smallest number in a list, The result of the Min() or Max() method call will be the minimum or maximum of all of these numerical values, as calculated for each element in the sequence. The� You can look at the implementation there, but basically it's a case of iterating through the data, remembering the maximum element we've seen so far and the maximum value it produced under the projection. In your case you'd do something like: var item = items.MaxBy(x => x.Height);

Min and Max - Using C# LINQ - A Practical Overview, It acts upon an array of any type, and returns the position of the value. C# program that uses IndexOf on Array using System; class Program { static void Main() Also: If you wanted a high-level collection it would be better to use the List type. LINQ has made it easy to find the maximum value from a given data source using Max() function. Otherwise, in our normal coding, we have to write a quite bit of code to get maximum value from list of values. Following is the syntax of using the LINQ Max() function to get maximum value from the list or collection in c# and

C# Array.IndexOf, LastIndexOf: Search Arrays, Use Max and Min from System.Linq. Get the highest or lowest elements. This is because 1 is the largest value in the program's array. However: When the Math� This program instantiates an array of 4 integers on the heap. Next the Max method is called upon this array. Finally we use another version of Max that receives a lambda expression. Return: The Max method returns 1 when called with no parameters. This is because 1 is the largest value in the program's array.

  • I wonder if people are actually looking for this when searching for this question? System.Array.IndexOf(score, score.Max()) Just saw a Unity dev use the below LINQ code for this simple task and I was face palming.
  • It is Linq to Objects, Pascal.
  • @Pascal: How do you define LINQ, exactly? To me, one of the nice things about LINQ is that you can add your own operators which work smoothly with the predefined ones. Editing for performance issues.
  • @Jon: Got it! I was out of the track. That being said. Elegant solution!
  • That's a great answer Jon - thanks. I tend to refer to extension methods like this LINQ but I'm guessing that I'd lose a semantic argument if that's what it came down to.
  • @JonSkeet Inspired by your criticism, I just modified it as if ((value!= null && value.CompareTo(maxValue) > 0) || maxIndex == -1) That should do it.
  • In my book this should be the accepted answer - why a dozen lines when one will do?
  • ...and the slowest also... .About the never notice: we all notice the Windows is slow despite of the fact "it is not dealing with huge arrays"
  • But this is not the slowest of all the solutions posted here. Also, could you please explain what you mean by "the Windows is slow"?
  • I felt myself intrigued and tested the performance of PJ7's proposal with that of the extension method. The task was to find the index of the max element (standard string comparison) in a list of random strings (8 to 32 characters long). The length of the list was up to 10000000. The results were statistically the same, e.g. for 1e7 strings of length 32 the extension method needed 1400 ms while that of PJ7 needed 1530. (Tested with Stopwatch in a unit test, debug mode, Windows 10, Intel i7, 2.8 GHz, RAM 32 GB).
  • Another remark: the code line written exactly as quoted above will throw an exception if scores is null or InvalidOperationException if the array is empty. So kind of try/catch will be needed.