Min and Max in a List in Java
Given an unsorted list of integers, find maximum and minimum values in it.
Input : list = [10, 4, 3, 2, 1, 20]
Output : max = 20, min = 1
Input : list = [10, 400, 3, 2, 1, -1]
Output : max = 400, min = -1
Sorting
This is least efficient approach but will get the work done. The idea is to sort the list in natural order, then the first or last element would be the minimum and maximum element respectively. Below’s the implementation in Java.
Implementation:
JAVA
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GFG {
public static Integer findMin(List<Integer> list)
{
if (list == null || list.size() == 0 ) {
return Integer.MAX_VALUE;
}
List<Integer> sortedlist = new ArrayList<>(list);
Collections.sort(sortedlist);
return sortedlist.get( 0 );
}
public static Integer findMax(List<Integer> list)
{
if (list == null || list.size() == 0 ) {
return Integer.MIN_VALUE;
}
List<Integer> sortedlist = new ArrayList<>(list);
Collections.sort(sortedlist);
return sortedlist.get(sortedlist.size() - 1 );
}
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>();
list.add( 44 );
list.add( 11 );
list.add( 22 );
list.add( 33 );
System.out.println( "Min: " + findMin(list));
System.out.println( "Max: " + findMax(list));
}
}
|
Collections.max()
Collections.min() method return 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.
Implementation:
JAVA
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GFG {
public static Integer findMin(List<Integer> list)
{
if (list == null || list.size() == 0 ) {
return Integer.MAX_VALUE;
}
return Collections.min(list);
}
public static Integer findMax(List<Integer> list)
{
if (list == null || list.size() == 0 ) {
return Integer.MIN_VALUE;
}
return Collections.max(list);
}
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>();
list.add( 44 );
list.add( 11 );
list.add( 22 );
list.add( 33 );
System.out.println( "Min: " + findMin(list));
System.out.println( "Max: " + findMax(list));
}
}
|
Naive
Here is naive way of finding find minimum and maximum value in an unsorted list where we check against all values present in the list and maintain minimum & maximum value found so far.
Implementation:
JAVA
import java.util.ArrayList;
import java.util.List;
public class GFG {
public static Integer findMin(List<Integer> list)
{
Integer min = Integer.MAX_VALUE;
for (Integer i : list) {
if (min > i) {
min = i;
}
}
return min;
}
public static Integer findMax(List<Integer> list)
{
Integer max = Integer.MIN_VALUE;
for (Integer i : list) {
if (max < i) {
max = i;
}
}
return max;
}
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>();
list.add( 44 );
list.add( 11 );
list.add( 22 );
list.add( 33 );
System.out.println( "Min: " + findMin(list));
System.out.println( "Max: " + findMax(list));
}
}
|
Last Updated :
11 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...