Java How To Find Min and Max in Array
Find the Minimum and Maximum Element in an Array
Go through the array and keep track of the highest and lowest values:
Example
int[] numbers = {45, 12, 98, 33, 27};
int max = numbers[0];
int min = numbers[0];
for (int n : numbers) {
if (n > max) {
max = n;
}
if (n < min) {
min = n;
}
}
System.out.println("Max: " + max);
System.out.println("Min: " + min);
Explanation: Start by assuming the first element is both the maximum and minimum. As you loop through the array, update max
if you find a larger number and min
if you find a smaller number.