Find minimum and maximum elements in an array in Java
This post will discuss how to find the minimum and maximum element in an array in Java.
1. Using List
If the given array is a non-primitive array, we can use Arrays.asList()
that returns a list backed by the array. Then we call the min()
and max()
methods of the Collections
class to get minimum and maximum elements, respectively. Notice that this does not perform an actual copy on array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { // non-primitive integer array Integer[] A = { 6, 8, 3, 5, 1, 9 }; List<Integer> ints = Arrays.asList(A); System.out.println("Min element is " + Collections.min(ints)); System.out.println("Max element is " + Collections.max(ints)); } } |
For primitive arrays, we can use Java 8 Stream for converting the array to a list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { // primitive integer array int[] A = { 6, 8, 3, 5, 1, 9 }; List<Integer> ints = Arrays.stream(A) .boxed() .collect(Collectors.toList()); System.out.println("Min element is " + Collections.min(ints)); System.out.println("Max element is " + Collections.max(ints)); } } |
2. Using Guava Library
The Guava library has Ints
, Doubles
, Chars
, Longs
, etc., classes that offer several static utility methods pertaining to primitives that are not already found in either Integer
or Arrays
class. To find the minimum and maximum element, we can use the min()
and max()
methods of the corresponding class.
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.primitives.Ints; class Main { public static void main(String[] args) { int[] A = { 6, 8, 3, 5, 1, 9 }; System.out.println("Min element is " + Ints.min(A)); System.out.println("Max element is " + Ints.max(A)); } } |
3. Using Java 8 Stream
With the introduction of Stream with Java 8, we can convert the array into the corresponding type stream using the Arrays.stream()
method. Then we can call the max()
and min()
method, which returns the maximum and minimum element of this stream as OptionalInt
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] A = { 6, 8, 3, 5, 1, 9 }; int max = Arrays.stream(A) .max() .getAsInt(); int min = Arrays.stream(A) .min() .getAsInt(); System.out.println("Min element is " + min); System.out.println("Max element is " + max); } } |
We can also get stream without using the Arrays.stream()
method, as shown below. Here, the idea is to get a stream of array indices and map every index to the corresponding element in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.stream.IntStream; class Main { public static void main(String[] args) { int[] A = { 6, 8, 3, 5, 1, 9 }; int max = IntStream.range(0, A.length) .map(i -> A[i]) .max() .getAsInt(); int min = IntStream.range(0, A.length) .map(i -> A[i]) .min() .getAsInt(); System.out.println("Min element is " + min); System.out.println("Max element is " + max); } } |
Finally, we can call the summaryStatistics()
method on a stream of numeric values, which returns IntSummaryStatistics
describing various summary data about the elements of this stream. To get the minimum and maximum element, call getMin()
and getMax()
methods on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.IntSummaryStatistics; class Main { public static void main(String[] args) { int[] A = { 6, 8, 3, 5, 1, 9 }; IntSummaryStatistics stats = Arrays.stream(A).summaryStatistics(); System.out.println("Min element is " + stats.getMin()); System.out.println("Max element is " + stats.getMax()); } } |
This is equivalent to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.IntSummaryStatistics; class Main { public static void main(String[] args) { int[] A = { 6, 8, 3, 5, 1, 9 }; IntSummaryStatistics stats = new IntSummaryStatistics(); for (int i : A) { stats.accept(i); } System.out.println("Min element is " + stats.getMin()); System.out.println("Max element is " + stats.getMax()); } } |
4. Write your own utility method
We can also write our own routine for finding the minimum and maximum element in the array, as demonstrated below:
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 26 27 28 29 30 |
class Main { private static int getMax(int[] A) { int max = Integer.MIN_VALUE; for (int i: A) { max = Math.max(max, i); } return max; } private static int getMin(int[] A) { int min = Integer.MAX_VALUE; for (int i: A) { min = Math.min(min, i); } return min; } public static void main(String[] args) { int[] A = { 6, 8, 3, 5, 1, 9 }; System.out.println("Min element is " + getMin(A)); System.out.println("Max element is " + getMax(A)); } } |
5. Using Sorting
Another solution is to sort the given array in the natural order. The minimum element would then be the first element, and the maximum element is the last element of the array. This option is not recommended as the time taken by the sorting routine will not be linear, and this also modifies the original array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] A = { 6, 8, 3, 5, 1, 9 }; Arrays.sort(A); System.out.println("Min element is " + A[0]); System.out.println("Max element is " + A[A.length - 1]); } } |
That’s all about finding the minimum and maximum elements in an array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)