Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array.Max(); for the array index rather than the value

Tags:

arrays

c#

I am creating an array that will take in 24 numbers and display them in a table. I have used "arrayname".Max(); to determine the highest number but I need to display the array slot with the highest number

e.g. hour 15 had the highest number so 15 will displayed in a message rather than the number assigned to 15.

My code is as follows:

 public void busiest(int[] A)
 {
     int busy;
     busy = A.Max(); //Displays the highest values in a given set i.e. an array
     Console.WriteLine("\nThe busiest time of day was hour " + busy);
 }

Could anyone say if i'm missing something simple to display the slot rather than the assigned number?

Thanks

like image 836
William. Foster Avatar asked Mar 07 '23 20:03

William. Foster


1 Answers

That you need to call is Array.IndexOf:

Array.IndexOf(A, A.Max());

For further info regarding this method, please have a look here.

Beware that if there are more than one elements in the array with the same value, the index of the first of them would be returned from this method. For instance if the maximum value is 10 and there are two elements one at position with index 2 and one at position with index 3, then this method would return the value of 2.

like image 134
Christos Avatar answered Mar 15 '23 00:03

Christos