Find minimum and maximum values in a java array

Let’s learn to find minimum and maximum values in a java array.

Find minimum and maximum values in a java array

To find minimum and maximum values in an array first we’re going to create two static methods minNumber() and mNumber().

One to find minimum value in an array and another to find maximum (max) value in an array.

Also read – bubble sort in java

Here’s the java program to find maximum and minimum number in an array or find minimum and maximum (max) values in a java array.

public class MaxMinValueInArray
{
   public static void main(String[] args) 
   {
      int[] arrNumber = new int[]{50, 150, 250, 500, 160, 530};
      int maximum = maxNumber(arrNumber);
      System.out.println("Maximum value : " + maximum);
      // calling min function java
      int minimum = minNumber(arrNumber);
      System.out.println("Minimum value : " + minimum);
   }
   // java find max in array
   public static int maxNumber(int[] arr)
   {
      int maximumValue = arr[0];
      for(int a = 1; a < arr.length; a++)
      {
         if(arr[a] > maximumValue)
         {
            maximumValue = arr[a];
         }
      }
      return maximumValue;
   }
   // java min of array
   public static int minNumber(int[] arr)
   {
      int minimumValue = arr[0];
      // find minimum value in array java
      for(int a = 1; a < arr.length; a++)
      {
         if(arr[a] < minimumValue)
         {
            minimumValue = arr[a];
         }
      }
      return minimumValue;
   }
}


Output:

find minimum and maximum values in a java array

Java program to find maximum and minimum number without using array

In the below example we are using math.max() method (java math.max array) and math.min() method find or get maximum (max) value and minimum number in an array.

Here’s the java program to find maximum and minimum number without using array.

import java.util.Scanner;
public class MaximumMinimumWithoutArray
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter total number: ");
      int numbers = sc.nextInt();
      int maximum = Integer.MIN_VALUE;
      int minimum = Integer.MAX_VALUE;
      System.out.println("Please enter " + numbers + " numbers.");
      for(int a = 0; a < numbers; a++)
      {
         int current = sc.nextInt();
         if(current > maximum)
         {
            maximum = current;
         }
         if(current < minimum)
         {
            minimum = current;
         }
      }
      System.out.println("largest of " + numbers + " numbers is: " + maximum);
      System.out.println("smallest of " + numbers + " numbers is: " + minimum);
      sc.close();
   }
}


Output:

Please enter total number:
5
Please enter 5 numbers.
96
65
86
26
41
largest of 5 numbers is: 96
smallest of 5 numbers is: 26


Find minimum value in array recursion java

Here’s the java program to find minimum value in array recursion java.

public class MinimumValueUsingRecursion 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = {28, 18, 29, 38, 98, 27, 80}; 
      int num = arrNumbers.length;
      // here we are calling function 
      System.out.println(findMinimumValue(arrNumbers, num));
   }
   public static int findMinimumValue(int[] arr, int number) 
   {
      if(number == 1) 
         return arr[0];
      return Math.min(arr[number - 1], findMinimumValue(arr, number - 1)); 
   }
}


Output:

18


Find index of max value in array java

To find index of max (maximum) value in array java first assign given numbers to an integer array “arrNumbers”.

Then using for loop iterate the given array and check maximum value or largest number is smaller than given value.

If the result is true reassign maximum value and index position. Finally print the index position on the console.

Here’s the program to find index of max value in array java.

public class IndexOfMaxValue
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[] { 26, 93, 58, 38, 90, 69, 29 };
      int maxValue = arrNumbers[0];
      int index = 0;
      for(int a = 0; a < arrNumbers.length; a++) 
      {
         if(maxValue < arrNumbers[a]) 
         {
            maxValue = arrNumbers[a];
            index = a;
         }
      }
      System.out.println("Index of max value in array java: " + index);
   }
}


Output:

Index of max value in array java: 1


find max number in array java

To find max number or largest number in array first we have to sort the given array.

Let’s see java program to find max number in array.

public class MaxNumberInArray
{
   public static int maximumNumber(int[] arrNumber, int total)
   {
      int temp;
      for(int a = 0; a < total; a++)
      {
         for(int b = a + 1; b < total; b++)
         {
            if(arrNumber[a] > arrNumber[b])
            {
               temp = arrNumber[a];
               arrNumber[a] = arrNumber[b];
               arrNumber[b] = temp;
            }
         }
      }
      return arrNumber[total - 1];
   }
   public static void main(String[] args)
   {
      int[] m = {10, 21, 50, 61, 30, 21};
      int[] n = {85, 86, 28, 46, 38, 73, 93};
      System.out.println("Max number in array: " + maximumNumber(m, 6));
      System.out.println("Max number in array: " + maximumNumber(n, 7));
   }
}


Output:

Max number in array: 61
Max number in array: 93