Open In App

Min and Max in a List in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




// This java program find minimum and maximum value
// of an unsorted list of Integer by using Collection
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
  
public class GFG {
  
    // function to find minimum value in an unsorted
    // list in Java using Collection
    public static Integer findMin(List<Integer> list)
    {
  
        // check list is empty or not
        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 natural order
        Collections.sort(sortedlist);
  
        // first element in the sorted list
        // would be minimum
        return sortedlist.get(0);
    }
  
    // function return maximum value in an unsorted
    // list in Java using Collection
    public static Integer findMax(List<Integer> list)
    {
  
        // check list is empty or not
        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 natural order
        Collections.sort(sortedlist);
  
        // last element in the sorted list would be maximum
        return sortedlist.get(sortedlist.size() - 1);
    }
  
    public static void main(String[] args)
    {
  
        // create an ArrayList Object list
        List<Integer> list = new ArrayList<>();
  
        // add element in ArrayList object list
        list.add(44);
        list.add(11);
        list.add(22);
        list.add(33);
  
        // print min and max value of ArrayList
        System.out.println("Min: " + findMin(list));
        System.out.println("Max: " + findMax(list));
    }
}


Output

Min: 11
Max: 44

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




// This java program find minimum and maximum value
// of an unsorted list of Integer by using Collection
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
  
public class GFG {
  
    // function to find minimum value in an unsorted
    // list in Java using Collection
    public static Integer findMin(List<Integer> list)
    {
  
        // check list is empty or not
        if (list == null || list.size() == 0) {
            return Integer.MAX_VALUE;
        }
  
        // return minimum value of the ArrayList
        return Collections.min(list);
    }
  
    // function return maximum value in an unsorted
    // list in Java using Collection
    public static Integer findMax(List<Integer> list)
    {
  
        // check list is empty or not
        if (list == null || list.size() == 0) {
            return Integer.MIN_VALUE;
        }
  
        // return maximum value of the ArrayList
        return Collections.max(list);
    }
  
    public static void main(String[] args)
    {
  
        // create an ArrayList Object list
        List<Integer> list = new ArrayList<>();
  
        // add element in ArrayList object list
        list.add(44);
        list.add(11);
        list.add(22);
        list.add(33);
  
        // print min and max value of ArrayList
        System.out.println("Min: " + findMin(list));
        System.out.println("Max: " + findMax(list));
    }
}


Output

Min: 11
Max: 44

 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




// This java program find minimum and maximum value
// of an unsorted list of Integer
import java.util.ArrayList;
import java.util.List;
  
public class GFG {
  
    // Naive function to find minimum value in an
    // unsorted list in Java
    public static Integer findMin(List<Integer> list)
    {
        // initialize min to some maximum value
        Integer min = Integer.MAX_VALUE;
  
        // loop through every element in the list and
        // compare min found so far with current value
        for (Integer i : list) {
  
            // update min if found to be more than
            // the current element
            if (min > i) {
                min = i;
            }
        }
  
        return min;
    }
  
    // This function return maximum value in an
    // unsorted list in Java
    public static Integer findMax(List<Integer> list)
    {
        // initialize max variable to minimum value
        Integer max = Integer.MIN_VALUE;
  
        // loop for compare to current max value
        // with all list element and find maximum value
        for (Integer i : list) {
  
            // update max if found to be less than
            // the current element
            if (max < i) {
                max = i;
            }
        }
  
        return max;
    }
  
    public static void main(String[] args)
    {
        // create an ArrayList Object list
        List<Integer> list = new ArrayList<>();
        // add element in ArrayList object list
        list.add(44);
        list.add(11);
        list.add(22);
        list.add(33);
  
        // print min and max value of ArrayList
        System.out.println("Min: " + findMin(list));
        System.out.println("Max: " + findMax(list));
    }
}


Output

Min: 11
Max: 44


Last Updated : 11 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads