C# Find Highest Array Value and Index

C# find highest array value and index

This is not the most glamorous way but works.

(must have using System.Linq;)

 int maxValue = anArray.Max();
int maxIndex = anArray.ToList().IndexOf(maxValue);

Get largest element in array in C#

You can loop through the array to get the maximum value and you only need to retain the largest value found thus far, so:

int max = Int32.MinValue;
for (var i = 0; i < array.Length; i++)
{
if (array[i] > max) {
max = array[i];
}
}
Console.WriteLine(max);

You can also use .Max() and that does this in the background.

How to get the index of the biggest number in array?

You can use IEnumerable.Max() method like;

Returns the maximum value in a sequence of values.

float[] sizeEdge = new float[] { 12f, 43f, 556f, 98f };
int maxIndex = Array.IndexOf(sizeEdge, sizeEdge.Max());
Console.WriteLine(sizeEdge[maxIndex]);

Result will be;

556

Here a DEMO.

Get top 3 highest values from array with index

You can create a temp class with index and value and then sort by this class members. Something like this code:

var result = wineOfferCounter
.Select((v, i) => new { v, i })
.OrderByDescending(x => x.v)
.ThenByDescending(x => x.i)
.Take(3)
.ToArray();

then iterate through this collection:

foreach (var entry in result)
{
Console.WriteLine("Offer " + entry.i + " bought " + entry.v + " times");
}

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

I suggest writing your own extension method (edited to be generic with an IComparable<T> constraint.)

public static int MaxIndex<T>(this IEnumerable<T> sequence)
where T : IComparable<T>
{
int maxIndex = -1;
T maxValue = default(T); // Immediately overwritten anyway

int index = 0;
foreach (T value in sequence)
{
if (value.CompareTo(maxValue) > 0 || maxIndex == -1)
{
maxIndex = index;
maxValue = value;
}
index++;
}
return maxIndex;
}

Note that this returns -1 if the sequence is empty.

A word on the characteristics:

  • This works with a sequence which can only be enumerated once - this can sometimes be very important, and is generally a desirable feature IMO.
  • The memory complexity is O(1) (as opposed to O(n) for sorting)
  • The runtime complexity is O(n) (as opposed to O(n log n) for sorting)

As for whether this "is LINQ" or not: if it had been included as one of the standard LINQ query operators, would you count it as LINQ? Does it feel particularly alien or unlike other LINQ operators? If MS were to include it in .NET 4.0 as a new operator, would it be LINQ?

EDIT: If you're really, really hell-bent on using LINQ (rather than just getting an elegant solution) then here's one which is still O(n) and only evaluates the sequence once:

int maxIndex = -1;
int index=0;
double maxValue = 0;

int urgh = sequence.Select(value => {
if (maxIndex == -1 || value > maxValue)
{
maxIndex = index;
maxValue = value;
}
index++;
return maxIndex;
}).Last();

It's hideous, and I don't suggest you use it at all - but it will work.



Related Topics



Leave a reply



Submit