Friday, December 27, 2024
Google search engine
HomeData Modelling & AIMin and Max in a List in Java

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




// 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
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments