One of the most common array tasks is finding the largest value. The pattern works by assuming the first element is the maximum, then scanning the rest:
int[] nums = {34, 12, 78, 56, 91, 23};
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
System.out.println(max); // 91
The loop starts at index because you already stored index in max. Each iteration compares the current element to the running maximum and updates it if the current element is larger.
To find the minimum instead, change the > to <. Everything else stays the same.