Reputation: 13614
I have this array:
float[] sizeEdge = new float[12,43,556,98];
Maybe my question will seem naive to some of ou but, Im a newer in .NET, I need to get the array index of the biggest number.
How do I implement it?
Thank you in advance.
Upvotes: 1
Views: 1777
Reputation: 144
float max = 0;
int key = 0;
for(int x=0;x<sizeEdge.Length;x++){
if(max<sizeEdge[x]){
max=sizeEdge;
key=x;
}
}
Upvotes: 2
Reputation: 18474
You can use the alternative version of select
.
float[] sizeEdge = new float[12,43,556,98];
var maxIndex = sizeEdge.Select((x,i) => new { value = x, index = i})
.OrderByDescending(x => x.value)
.Select(x=> x.index)
.FirstOrDefault();
Upvotes: 1
Reputation: 144136
float max = sizeEdge.Max();
int maxIndex = Array.IndexOf(sizeEdge, max);
Note this will iterate the array twice, and will throw an exception if the array is empty. If you need to handle these, a loop is probably cleaner. You could create an extension method:
public static int IndexOfMax<T>(this IEnumerable<T> seq) where T : IComparable<T>
{
if(! seq.Any()) return -1;
T max = seq.First();
int maxIdx = 0;
int idx = 1;
foreach(T item in seq.Skip(1))
{
if(max.CompareTo(item) < 0)
{
max = item;
maxIdx = idx;
}
++idx;
}
return maxIdx;
}
or a linq version:
public static int IndexOfMax<T>(this IEnumerable<T> seq) where T : IComparable<T>
{
return seq.Select((f, idx) => new { Val = f, Idx = idx })
.Aggregate(new { Max = default(T), MaxIndex = -1 }, (mp, fp) =>
{
return mp.MaxIndex == -1 || fp.Val.CompareTo(mp.Max) > 0 ? new { Max = fp.Val, MaxIndex = fp.Idx } : mp;
}).MaxIndex;
}
Upvotes: 5
Reputation: 109567
This should work; generalised to IEnumerable<float>
:
// Returns index of maximum value, or -1 if there are no numbers.
int IndexOfMax(IEnumerable<float> numbers)
{
float max = 0;
int indexOfMax = -1;
int index = 0;
foreach (var number in numbers)
{
if ((number > max) || (indexOfMax < 0))
{
indexOfMax = index;
max = number;
}
++index;
}
return indexOfMax;
}
This only traverses the array once, rather than twice like what happens if you use both .Max()
and IndexOf()
so it should be somewhat faster.
You can generalise this to types that implement IComparable<T>
like so:
// Returns index of maximum value, or -1 if there are no numbers.
int IndexOfMax<T>(IEnumerable<T> numbers) where T: IComparable<T>
{
T max = default(T);
int indexOfMax = -1;
int index = 0;
foreach (var number in numbers)
{
if ((number.CompareTo(max) > 0) || (indexOfMax < 0))
{
indexOfMax = index;
max = number;
}
++index;
}
return indexOfMax;
}
Upvotes: 1
Reputation: 98750
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
.
Upvotes: 1
Reputation: 63065
float[] sizeEdge = new float[]{12f, 43f, 556f, 98f};
var result = Array.IndexOf(sizeEdge,sizeEdge.Max());
Upvotes: 2
Reputation: 10427
You can use Linq.
Don't forget the using statement using System.Linq;
int indexOfMax = Array.IndexOf(sizeEdge, sizeEdge.Max());
Upvotes: 1