Three ways to find minimum and maximum values in a Java array.
Apr 13
Java Array, Java, maximum, minimum 43 Comments

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 the most?
Image used in this post:
ByTheTruthAbout , published under Creative Commons license CC BY-SA 2.0
Related posts:



May 02, 2008 @ 08:39:30
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
Jul 03, 2008 @ 01:22:36
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!
Jul 08, 2008 @ 12:07:35
Thanks for the code. Thank you very much.
May 06, 2009 @ 17:15:55
I am just surprised why your code has Case difference in the upper box and the lower boxes. Couldn’t get ther result that way
Jun 11, 2009 @ 02:48:05
Interesting – but of course the sort does more than merely find the maximum and minimum, and also involves correspondingly more work. Obviously, this is most relevant when the operation is to be used many thousands of times a second.
Aug 07, 2009 @ 04:37:49
galing niyo
Jan 15, 2010 @ 03:02:16
wala bang mas mababa na code para makuha yan?
Feb 21, 2010 @ 08:19:53
Thanks for the code. Your ready made solution saved my time.
Feb 21, 2010 @ 10:53:04
Manish, I am glad you found my code useful.
Apr 01, 2010 @ 07:43:38
Object obj Collections.max(Your Arraylist)
Apr 12, 2010 @ 08:19:41
Your version of the straight iteration is a bit convoluted:
int getMax(int[] numbers) {
int max= Integer.MIN_VALUE;
for (int i : numbers)
max = Math.max(max, i);
return max;
}
And the straight iteration is in fact the most efficient method, as using sort uses n*log(n) time and the recursive variant uses more resources on the stack.
May 04, 2010 @ 00:57:23
yeah it was a great explanation i understood it. thanks and be success!!!!!
May 30, 2010 @ 07:24:55
how bout a code using for-loop ?
Jun 21, 2010 @ 13:12:51
Thanks 4 solution.it helps me to save my time.
Jul 29, 2010 @ 09:09:19
Thanks, Saved me some time.
Aug 24, 2010 @ 20:35:01
teach me more about arrays cous i realy dont understand it…
help me.. im napoleon the great pretender..
Oct 05, 2010 @ 05:02:28
Superb ra nee yabba…
Oct 09, 2010 @ 10:50:56
Nice! help out alot but i have to remove the 36.public static int getMinValue(int[] numbers){
thanks !
Nov 14, 2010 @ 11:32:22
you give me very helpful information
thank u very much
Dec 12, 2010 @ 18:26:22
Thanks so much. Very helpful. I am bookmarking this!
Dec 14, 2010 @ 23:42:06
Thank you very much for taking the time to post these methods. I must have gone through 3 different wordings of my search in google and 20 different websites before I found this website. The previous 20 websites were just forums full of non-helpful people that assume everyone knows this and use lots of jargon that basically makes this far more complicated than needed.
Jan 01, 2011 @ 07:16:30
Could you please tell me that how to find Max and Min Array through recursion (data structure) ?
please send me the implementation …
Jan 24, 2011 @ 02:51:32
in case you have to consider performance do not use sort, because this runs in O(n log n) time.
Collections.max should be prefered.
Jan 26, 2011 @ 12:38:40
Used to get the Max and Min of integer array in one Iteration.
public class TestMinMaxIntegerArray {
/**
* @param args
*/
public static void main(String[] args) {
int[] xx = { -3, 0,10, 1, 1 };
int min = 0;
int max = 0;
int tempMax = 0;
int tempMin = 0;
boolean flag = true;
for (int i = 0, j = 1; j < xx.length; i++, j++) {
if (xx[i] min) {
tempMin = min;
}
if (tempMax < max) {
tempMax = max;
}
}
System.out.println("MAX:" + tempMax + " " + "MIN:" + tempMin);
}
}
Jan 26, 2011 @ 12:43:30
Sorry guys for the post…Its not a complete code
Jan 26, 2011 @ 12:45:28
public static void main(String[] args) {
int[] xx = { -3, 0,10, 1, 1 };
int min = 0;
int max = 0;
int tempMax = 0;
int tempMin = 0;
boolean flag = true;
for (int i = 0, j = 1; j < xx.length; i++, j++) {
if (xx[i] min) {
tempMin = min;
}
if (tempMax < max) {
tempMax = max;
}
}
System.out.println("MAX:" + tempMax + " " + "MIN:" + tempMin);
}
Jan 26, 2011 @ 12:46:16
Sorry dont know why the data is getting truncated.
Feb 13, 2011 @ 07:10:00
Sorry, but I think I need some help here.. I can’t seems to get the minimum value but I can get the maximum value for my programme. Is this the complete code or something? Or you can somehow help me check through my programme on why I can’t get the desire result..?
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]);
Feb 14, 2011 @ 22:38:38
@SS what minimum value are you getting? I ran your program and got -3 as the minimum value, which is correct.
Jul 20, 2011 @ 20:46:37
This is the working code:
import java.io.*;
class maxx_minn
{
public static void main(String ags [ ])throws IOException
{
int i,max,min,num;
BufferedReader br= new BufferedReader
(new InputStreamReader (System.in));
i=1;
max=0;
min=2^31-1;
while(i<=10)
{
System.out.println("Enter a number");
num= Integer.parseInt (br.readLine());
if(maxnum)
min=num;
i++;
}
System.out.println(“THE MAXIMUM NUMBER IS:”+max);
System.out.println(“THE MINIMUM NUMBER IS:”+min);
}
}
Aug 28, 2011 @ 12:45:24
good one amazing….job
Sep 13, 2011 @ 04:03:47
import java.util.* ;
class min
{
public static void main(String a1[])
{
Scanner in=new Scanner(System.in);
int i,mn=0,mx=0,a[];
a=new int[5];
System.out.println(“Enter elements of array”);
for(i=0;i<a.length;i++)
{
a[i]=in.nextInt();
if(mx<a[i])
{
mx=a[i];
}
}
mn=mx;
for(i=0;ia[i])
{
mn=a[i];
}
}
System.out.println(“min = “+mn);
}
}
Sep 13, 2011 @ 04:07:36
import java.util.* ;
class min
{
public static void main(String a1[])
{
Scanner in=new Scanner(System.in);
int i,min=0,max=0,a[];
a=new int[5];
System.out.println(“Enter elements of array”);
for(i=0;i<a.length;i++)
{
a[i]=in.nextInt();
if(max<a[i])
{
max=a[i];
}
}
min=max;
for(i=0;ia[i])
{
min=a[i];
}
}
System.out.println(“maxmium =”+max);
System.out.println(“miniumum = “+min);
}
}
Sep 13, 2011 @ 04:13:11
import java.util.* ;
class min
{
public static void main(String a1[])
{
Scanner in=new Scanner(System.in);
int i,min=0,max=0,a[];
a=new int[5];
System.out.println(“Enter elements of array”);
for(i=0;i<a.length;i++)
{
a[i]=in.nextInt();
if(max<a[i])
{
max=a[i];
}
}
min=max;
for(i=0;ia[i]);
min=a[i];
}
}
System.out.println(“maxmium =”+max);
System.out.println(“miniumum = “+min);
}
}
Sep 14, 2011 @ 19:28:54
great job
Sep 18, 2011 @ 18:34:08
Thanks, had to use part of this in a assignment,helped a lot.
Cheers!
Sep 29, 2011 @ 07:37:53
please provide me the following source code of java Programming.
Create an Array of ten cells, store the value in the area at run time. Find the find the largest and smallest element.
Sep 29, 2011 @ 07:54:04
create an array of ten cells, store the value in the area at run tim. Find the largest and smallest element.
Oct 20, 2011 @ 01:17:59
I’ve been helped a great deal. thank you all of you guys!!
Nov 22, 2011 @ 05:38:31
import java.util.Scanner;
public class Array {
public static void main(String[]args)
{
int [][]marks=new int [2][2];
fillArray(marks,2,2);
max(marks,2,2);
}
public static void fillArray(int mmarks[][],int row ,int coloumn){
int k=0;
Scanner input=new Scanner(System.in);
for (int i = 0; i < row ; i++){
for(int j=0 ; j < coloumn ; j++){
k=k+1;
System.out.println("Enter marks"+k);
mmarks[i][j]=input.nextInt();
}
}
}
public static void max(int mnmarks[][],int nrow,int ncoloumn){
int maxValue=mnmarks[0][0];
for (int i = 0; i < nrow ; i++){
for (int j = 0 ; j maxValue){
maxValue=mnmarks[i][j];
}
}
}
System.out.println(“highest=”+maxValue);
}
}
Jan 10, 2012 @ 21:36:08
Thank you so much. Made the concept much clearer. Thanks again.
Keep up the good work.
Jan 11, 2012 @ 11:40:18
hi
i need some information about testing c programming
is anybody can help me what i have to do?
i have my program but i donot know where i have to test it
many thanks