identifying element number in array, after finding max

I am collecting integers from a few objects via getcomponent, and putting them into an array, then I want to determine the one with the maximum.
I know how to get the maximum integer in the array. I did it using using System.Linq and myarray.Max(); The only reason I am putting them in the array was for an easy method to compare them to determine this highest number.

But then I still do not know by doing that which one of them had that maximum, which is my whole purpose for doing it.

Do I then need to cycle through them all again with if statements to see which one equalled that maximum?
Ie. If myarray[0]=myarraymax // which belonged to Object A

If that is true, Then print (“the highest number belonged to Object A”), or go to the next If for Object B. My whole purpose is to determine if Object A,B, or C, (sourced from GetComponent), has the highest number.

It just seems a nuisance when I have already pulled the maximum integer out.

Is there a quicker way?

It seems so easy with one line of code to pull out the highest number, so I cannot see why it cannot be as easy to ask which one did it belong?
c# only please.

If you want to do it without Linq:

public int GetMaxArrayElement (int[] array, out int index) {
    index = -1;
    int max = int.MinValue;
    for (int i = 0; i < array.Length; ++i) {
        if (array *> max) {*

max = array*;*
index = i;
}
}
return max;
}
Usage:
int[] MyArray = {8, 6, 7, 5, 3, 0, 9};
int MaxIndex; // No need to initialize here, although you can
int MaxArrayValue = GetMaxArrayElement (MyArray, out MaxIndex);
if (MaxIndex < 0) {
// Something went wrong and we couldn’t get a max value
// Might need to do some cleanup. Obviously, this is overkill for this scenario.
} else { // optional
// All is good, carry on
}
Edit: replaced Mathf.NegativeInfinity with int.MinValue.

Another version :slight_smile:

public int GetMaxElement(int[] array, out int index)
{
	if (array==null) 
	{
		index = -1;
		return int.MinValue;
	}
	int max = array.Max();
	index = System.Array.FindIndex(array, x=>x==max);
	return max;
}

Ok my answer so far is from myself Lol.

foreach (int i in array)
        {
            if (array *== array_max && array_max > 0)*

{
print(i);
}