find the minimum and maximum element in an array in java
Code snippet on how to find the minimum and maximum element in an array in java
int min = array[0];
int max = array[0];
for (int i = 1; i < array.length; i++){
if (array[i] > max){
max = array[i];
}
if (array[i] < min){
min = array[i];
}
}
This code is used to find the minimum and maximum elements in an array in Java. It requires that the array is already initialized. First, we set the initial min and max values to be the first element in the array. Then we loop through the array from the second element, and compare the current element with the min and max values from the previous iteration. If the new element is larger than the max value, we update the max value, and if the new element is smaller than the min value, we update the min value. After the loop has finished, the min and max values are the smallest and largest elements in the array, respectively.