find the maximum and minimum value of an array in java code example
Example 1: java function that returns the index of the largest value in an array
public int getIndexOfLargest( int[] array )
{
if ( array == null || array.length == 0 ) return -1; // null or empty
int largest = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[largest] ) largest = i;
}
return largest; // position of the first largest found
}
Example 2: index of maximum value in list python
[i for i, x in enumerate(l) if x == max(l)]