Three ways to find minimum and maximum values in a Java array.
In Java you can find maximum or minimum value in a numeric array by looping through the array. Here is the code to do that.
public static int getMaxValue(int[] numbers){
int maxValue = numbers[0];
for(int i=1;i < numbers.length;i++){
if(numbers[i] > maxValue){
maxValue = numbers[i];
}
}
return maxValue;
}
public static int getMinValue(int[] numbers){
int minValue = numbers[0];
for(int i=1;i<numbers.length;i++){
if(numbers[i] < minValue){
minValue = numbers[i];
}
}
return minValue;
}
These are very straight forward methods to get maximum or minimum value of an array but there is a more cleaner way to do this.
int[] nums={6,-1,-2,-3,0,1,2,3,4};
Arrays.sort(nums);
System.out.println(”Minimum = ” + nums[0]);
System.out.println(”Maximum = ” + nums[nums.length-1]);
You cannot make it more simpler than this. You will need to import java.util.* to use Arrays class.
Yet another way to get these values is using recursion. Today someone asked me about this and after thinking for few minutes I came up with following solution.
public static int getMax(int[] numbers, int a, int n){
return a>=numbers.length?n:Math.max(n,max(numbers,a+1,numbers[a]>n?numbers[a]:n));
}
private static int getMin(int[] numbers, int a, int n) {
return a==numbers.length?n:Math.min(n,min(numbers,a+1,numbers[a]<n?numbers[a]:n));
}
Here is an executable Java class demonstrating the use of all three method.
import java.util.Arrays;
public class MinMaxValues{
public static void main (String args[]){
int numbers[]= {1,5,-9,12,-3,89, 18,23,4,-6};
//Find minimum (lowest) value in array using loop
System.out.println(”Minimum Value = ” + getMinValue(numbers));
//Find maximum (largest) value in array using loop
System.out.println(”Maximum Value = ” + getMaxValue(numbers));
//Find minimum (lowest) value in array by sorting array
System.out.println(”Minimum Value = ” + minValue(numbers));
//Find maximum (largest) value in array by sorting array
System.out.println(”Maximum Value = ” + maxValue(numbers));
//Find minimum (lowest) value in array using recursion
System.out.println(”Minimum Value = ” + getMin(numbers,0,numbers[0]));
//Find maximum (largest) value in array using recursion
System.out.println(”Maximum Value = ” + getMax(numbers,0,numbers[0]));
}
//Find maximum (largest) value in array using loop
public static int getMaxValue(int[] numbers){
int maxValue = numbers[0];
for(int i=1;i<numbers.length;i++){
if(numbers[i] > maxValue){
maxValue = numbers[i];
}
}
return maxValue;
}
//Find minimum (lowest) value in array using loop
public static int getMinValue(int[] numbers){
int minValue = numbers[0];
for(int i=1;i<numbers.length;i++){
if(numbers[i] < minValue){
minValue = numbers[i];
}
}
return minValue;
}
//Find minimum (lowest) value in array using array sort
public static int minValue(int[] numbers){
Arrays.sort(numbers);
return numbers[0];
}
//Find maximum (largest) value in array using array sort
public static int maxValue(int[] numbers){
Arrays.sort(numbers);
return numbers[numbers.length-1];
}
//Find maximum (largest) value in array using recursion
public static int getMax(int[] numbers, int a, int n){
return a>=numbers.length?n:Math.max(n,getMax(numbers,a+1,numbers[a]>n?numbers[a]:n));
}
//Find minimum (lowest) value in array using recursion
private static int getMin(int[] numbers, int a, int n) {
return a==numbers.length?n:Math.min(n,getMin(numbers,a+1,numbers[a]<n?numbers[a]:n));
}
}
So which method do you like most?
If you enjoyed this post, make sure you subscribe to my RSS feed!
Related Posts:









Bret:
I really appreciate the fact you take the time to write about something that others “assume” everyone knows. Quick useful posts such as this really help more people than you might imagine. Keep them coming.
Bret’s last blog post..Friday Finds for 05/02/2008: Online RAID Space Calculation
Posted on May 2nd, 2008 at 8:39 am
Manh Nguyen:
Thanks for your code above, in my case, i have to find them from the list of integer input from keyboard.
Have you solutions help me.
Best regards!
Posted on July 3rd, 2008 at 1:22 am