To find maximum and minimum values in an array in Java you can use one of the following options-

- Iterate the array and look for the maximum and minimum values.
- You can also write a recursive method to recursively go through the array to find maximum and minimum values in an array.
- You can use Arrays.sort() method to sort the array and then take the first and last elements of that sorted array.

#### Iterating array to find largest and smallest values

Start by having two variables (max and min) with initial value as the first element of the array. Iterate through the array and compare current element with max variable, if current array element is greater than the max then assign the current element to max. Otherwise compare element with min variable, if current array element is less than the min then assign the current element to min.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class MaxMinArray { public static void main(String[] args) { int arr[] = {54, 24, -4, 0, 2, 45, 54, -9, 7}; // assign first array element to two variables int max = arr[0]; int min = arr[0]; // iterate and compare from array index 1 for(int i = 1; i < arr.length; i++){ if(max < arr[i]){ max = arr[i]; }else if(min > arr[i]){ min = arr[i]; } } System.out.println("Maximum number = " + max + " Minimum number = " + min); } } |

__Output__

1 |
Maximum number = 54 Minimum number = -9 |

#### Find maximum and minimum values in array using recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class MaxMinArray { public static void main(String[] args) { int arr[] = {54, 24, 4, 0, 2, 45, 55, 9, -7, 68}; int max = maxUsingRecursion(arr, arr[0], 0); int min = minUsingRecursion(arr, arr[0], 0); System.out.println("Maximum number = " + max + " Minimum number = " + min); } private static int maxUsingRecursion(int[] arr, int num, int size){ // base case if(size == arr.length){ return arr[size-1]; } return Math.max(num, maxUsingRecursion(arr, arr[size], ++size)); } private static int minUsingRecursion(int[] arr, int num, int size){ // base case if(size == arr.length) return arr[size-1]; return Math.min(num, minUsingRecursion(arr, arr[size], ++size)); } } |

__Output__

1 |
Maximum number = 68 Minimum number = -7 |

#### Find maximum and minimum values in array by sorting

1 2 3 4 5 6 7 8 9 10 |
public class MaxMinArray { public static void main(String[] args) { int arr[] = {54, 24, 4, 0, 2, 45, 55, 9, -7, 68}; Arrays.sort(arr); System.out.println("Maximum number = " + arr[arr.length - 1] + " Minimum number = " + arr[0]); } } |

__Output__

1 |
Maximum number = 68 Minimum number = -7 |

That’s all for the topic **Java Program to Find Maximum And Minimum Values in an Array**. If something is missing or you have something to share about the topic please write a comment.

**You may also like**

- Java Program to Find Common Element Between Two Arrays
- How to Unzip a File in Java
- PriorityBlockingQueue in Java
- Java ScheduledThreadPoolExecutor – Scheduling With ExecutorService
- Abstraction in Java – OOPS Concepts
- How to See Logs And Sysouts in Hadoop MapReduce