Find min and max values in an unsorted Integer List in Java
This post will discuss how to find the minimum and maximum value in an unsorted list of integers in Java without using Java 8 Stream.
1. Naive solution
Here’s a naive (but efficient) way of finding find minimum and maximum value in an unsorted list, where we check against all values present in the list and maintain the minimum and maximum value found so far.
Min
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Naive method to find the minimum value in an unsorted list in Java public static Integer getMin(List<Integer> list) { // initialize `min` to some maximum value Integer min = Integer.MAX_VALUE; // loop through every element in the list and // compare the minimum found so far with the current value for (Integer i: list) { // update min if found to be more than the current element if (min > i) { min = i; } } return min; } |
Max
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Naive method to find the maximum value in an unsorted list in Java public static Integer getMax(List<Integer> list) { // initialize `max` to some minimum value Integer max = Integer.MIN_VALUE; // loop through every element in the list and // compare the maximum found so far with the current value for (Integer i: list) { // update max if found to be less than the current element if (max < i) { max = i; } } return max; } |
2. Using Collections.max() method
Collections.min() method returns the minimum element in the specified collection, and Collections.max() returns the maximum element in the specified collection, according to the natural ordering of its elements.
Min
|
1 2 3 4 5 6 7 8 9 |
// Method to find the minimum value in an unsorted list in Java public static Integer getMin(List<Integer> list) { if (list == null || list.size() == 0) { return Integer.MAX_VALUE; } return Collections.min(list); } |
Max
|
1 2 3 4 5 6 7 8 9 |
// Method to find the maximum value in an unsorted list in Java public static Integer getMax(List<Integer> list) { if (list == null || list.size() == 0) { return Integer.MIN_VALUE; } return Collections.max(list); } |
Both these methods iterate over the entire list. Hence, they require time proportional to the size of the list.
3. Using Sorting
This is the least efficient approach but will get the work done. The idea is to sort the list in the natural order, and then the first or last element would be the minimum and maximum element, respectively. Following’s implementation in Java:
Min
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Method to find the minimum value in an unsorted list in Java public static Integer getMin(List<Integer> list) { if (list == null || list.size() == 0) { return Integer.MAX_VALUE; } // create a new list to avoid modification in the original list List<Integer> sortedList = new ArrayList<>(list); // sort list in the natural order Collections.sort(sortedList); // the first element in the sorted list would be minimum return sortedList.get(0); } |
Max
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Method to find the maximum value in an unsorted list in Java public static Integer getMax(List<Integer> list) { if (list == null || list.size() == 0) { return Integer.MIN_VALUE; } // create a new list to avoid modification in the original list List<Integer> sortedList = new ArrayList<>(list); // sort list in the natural order Collections.sort(sortedList); // the last element in the sorted list would be maximum return sortedList.get(sortedList.size() - 1); } |
That’s all about finding the min and max values in an unsorted Integer List 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 :)