Given a List, find the largest element in it. There are multiple approaches to tackle this problem like iterating through the List or using various inbuilt functions.
Input : List = [5, 3, 234, 114, 154] Output : 234 Input : List = {10, 20, 4} Output : 20
Approach 1: Using ForEach Loop
- Create List object and store multiple elements in it.
- Create one variable and initialize it with Integer.MAX_VALUE.
- Start iterating through the List using for each loop and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List import java.util.Arrays; import java.util.List; public class Test { public static void main(String[] args) { // List input List<Integer> arrayList = Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 ); // Create maxValue variable and initialize with 0 int maxValue = 0 ; // Check maximum element using for loop for (Integer integer : arrayList) { if (integer > maxValue) maxValue = integer; } System.out.println( "The maximum value is " + maxValue); } } |
The maximum value is 1540
Approach 2: Using Iterators
- Create List object and store multiple elements in it.
- Create one variable and initialize it with Integer.MAX_VALUE.
- Start iterating through the List using List iterator and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Test { public static void main(String[] args) { // List as input List<Integer> arrayList = Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 ); // List iterator Iterator listIterator = arrayList.iterator(); // Create maxValue variable and initialize with 0 Integer maxValue = 0 ; // Iterate list while (listIterator.hasNext()) { Integer integer = (Integer)listIterator.next(); // If value is greater then update maxValue if (integer > maxValue) maxValue = integer; } System.out.println( "The maximum value is " + maxValue); } } |
The maximum value is 1540
Approach 3: Using Indexing
- Create List object and store multiple elements in it.
- Create one variable and initialize it with Integer.MAX_VALUE.
- Start iterating through the List and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List import java.util.Arrays; import java.util.List; public class Test { public static void main(String[] args) { // List as input List<Integer> arrayList = Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 ); // Create maxValue variable and initialize with 0 Integer maxValue = 0 ; // Iterate List using for each loop for ( int i = 0 ; i < arrayList.size(); i++) { // If element is greater the update maxValue if (arrayList.get(i) > maxValue) maxValue = arrayList.get(i); } System.out.println( "The maximum value is " + maxValue); } } |
The maximum value is 1540
Approach 4: Using JDK 8
Java
// create maxValue variable and initialize with 0 import java.util.Arrays; import java.util.List; public class Test { public static void main(String[] args) { // List as input List<Integer> arrayList = Arrays.asList( 5 , 3 , 15 , 234 , 114 , 1540 ); // Initialize with inbuilt Max function int maxValue = arrayList.stream() .max(Integer::compareTo) .get(); System.out.println( "The maximum value is " + maxValue); } } |
The maximum value is 1540
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.