The Wayback Machine - https://web.archive.org/web/20250323023701/https://www.geeksforgeeks.org/java-program-for-program-to-find-largest-element-in-an-array/
Open In App

Java Program to Find Largest Element in an Array

Last Updated : 04 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The most common method to find and print the largest element of a Java array is to iterate over each element of the array and compare each element with the largest value.

Example:

Input : arr[] = {20, 10, 20, 4, 100}
Output : 100

Input : arr[] = {1000, 2000, 4000}
Output : 4000

Implementation: Here, we are using the iterative approach or you can say the naive approach.

Java
// Java Program to find the largest element in 
// array using iterative approach

class Geeks {
  
    // Array declared
    static int arr[] = {20, 10, 20, 4, 100}; 
    
    // Method to find maximum in arr[] 
    static int largest() {
      
        int i; 
        
        // Initialize maximum element 
        int max = arr[0]; 
        
          // Traversing and comparing max element
        for (i = 1; i < arr.length; i++) 
            if (arr[i] > max) 
                max = arr[i]; 
        
        return max; 
    } 
    
    public static void main(String[] args) {
      
        System.out.println(largest()); 
    } 
} 

Output
100

Complexity of the approach is O(N) and is best used for the small and normal sized arrays.

Other Methods to Find the Largest Element in an Array

  1. Java 8 Stream
  2. Sorting
  3. Using Collections.max()

1. Using Java 8 Stream (Used for Java 8+)

Best used for concise and parallelizable with other operations with the Time complexity of O(N).

Implementation:

Java
// Java Program to Find the Largest
// Element in Array using Java Stream
import java.util.Arrays; 

public class GFG 
{
    public static void main(String[] args)
    { 
          
        int arr[] = {20, 10, 20, 4, 100}; 
        
        // Java Stream and max to find the 
          // max element in array
        int max = Arrays.stream(arr).max().getAsInt(); 
      
        // Printing the result
        System.out.println(max); 
    } 

} 

Output
100

2. Sorting the Array (Used when Sorting is essential)

We can sort the array to get the largest element from the array. After sorting we can directly extract last element as largest element(Ascending order). We can use predefined sort method defined in util class. And, the complexity of the method is O( Nlog(N) ).

Implementation:

Java
// Java Program to find the largest element
// in the array using sort function
import java.io.*;
import java.util.*;

class GFG 
{
    public static void main(String[] args)
    {
          // Array created      
        int arr[] = {20, 10, 20, 4, 100};
          
        // Sorting function using sort function
        Arrays.sort(arr);
      
        // Printing the result
        System.out.println(arr[arr.length - 1]);
    }
}

Output
100

Note: implementation can be done with user-defined sorting method also.

3. Using Collections.max() (Used with ArrayList)

The max() method of java.util.Collections class is used to return the maximum element of the given collection, according to the natural ordering of its elements.

Implementation:

Java
// Java Program to find the maximum element
// in an Array using Collections.max() method
import java.util.*;

public class GFG 
{
    public static void main(String[] args)
    {      
        // Declaring array
        int arr[] = {20, 10, 20, 4, 100};

        // Creating new list
        List<Integer> list = new ArrayList<>();
      
        // Adding elements in list
        for (int i = 0; i < arr.length; i++) {
            list.add(arr[i]);
        }

        // Using the method to find the
        // maximum element
        System.out.println(Collections.max(list));
    }
}

Output
100

Please refer complete article on Program to find largest element in an array for more details!



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg