Open In App

Java Program to find largest element in an array

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, find the largest element in it.

Examples of Finding the largest element in an array

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

Methods to Find the Largest Element in an Array

There are certain methods to find the Largest Element in an Array as mentioned below:

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

1. Iterative Method (Brute Force Method)

Below is the implementation of the above method:

Java




// Java Program to find maximum in arr[]
 
// Driver Class
class Test
{
      // array declared
    static int arr[] = {10, 324, 45, 90, 9808};
     
    // Method to find maximum in arr[]
    static int largest()
    {
        int i;
         
        // Initialize maximum element
        int max = arr[0];
         
        // Traverse array elements from second and
        // compare every element with current max
        for (i = 1; i < arr.length; i++)
            if (arr[i] > max)
                max = arr[i];
         
        return max;
    }
     
    // Driver method
    public static void main(String[] args)
    {
        System.out.println("Largest in given array is " + largest());
    }
}


Output

Largest in given array is 9808


Complexity of the above method:

Time Complexity: O(n), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

2. Java 8 Stream

You can simply use the new Java 8 Streams but you have to work with int. 

Below is the implementation of the above method:

Java




// Java Program to Find the Largest
// Element in Array using Java Stream
import java.util.Arrays;
 
// Driver Class
public class GFG {
      // main function
    public static void main(String[] args){
        int arr[] = {10, 324, 45, 90, 9808};
         
          // Java Stream and max to find the max element
          // in array
          int max = Arrays.stream(arr).max().getAsInt();
       
          // Printing the result
        System.out.println("Largest in given array is " +max);
    }
 
}


Output

Largest in given array is 9808


Complexity of the above method:

Time Complexity: O(n), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

3. Sorting the Array

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).

i). User Defined Method:

User Defined Method is where we can provide the sort function which will sort the array .

Note: Sorting Algorithm used in the Program mentioned below is Insertion Sort, However we can use alternative sorting algorithm too.

Below is the implementation of the above method:

Java




// Java program to find maximum in
// arr[] of size n
import java.io.*;
import java.util.*;
 
// Driver Class
class Main{
   
    // Returns maximum in arr[] of size n
    static int largest(int arr[],int n)
    {
      for (int i = 1; i < n; ++i) {
          int key = arr[i];
          int j = i - 1;
 
          // Move elements of arr[0..i-1], that are
          // greater than key, to one position ahead
          // of their current position
          while (j >= 0 && arr[j] > key) {
              arr[j + 1] = arr[j];
              j = j - 1;
          }
          arr[j + 1] = key;
      }
 
      return arr[n-1];
    }
 
    // Main function
    static public void main(String[] args)
    {
        int[] arr = { 10, 324, 45, 90, 9808 };
        int n = arr.length;
        System.out.println(largest(arr, n));
    }
}


Output

9808


ii). Pre Defined Function

We can use sort function is a sorting algorithm.

Alternate way to write same Program  with predefined function :

Java




// Java Program to Find the Largest
// Element in the array using sort function
import java.io.*;
import java.util.*;
 
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        int arr[] = { 12,   456789100, 23,
                      3456, 897, 452, 444, 899, 700 };
           
          // Sorting function using
          // Sort function
        Arrays.sort(arr);
       
          // Printing the Result
        System.out.println(
            "Largest number from given array: "
            + arr[arr.length - 1]);
    }
}


Output

Largest number from given array: 3456


Complexity of the above method:

Time Complexity: O(n logn), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

4. Using Collections.max()

Define an empty ArrayList and add all elements of array to it.Pass this ArrayList to Collections.max().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.

Blow is the implementation of the above method:

Java




// Java Program to find the maximum
// element in an Array using
// Collections.max() method
import java.util.*;
 
// Driver Class
public class GFG {
    // main function
    public static void main(String[] args)
    {
        // Declaring Array
        int arr[] = { 10, 324, 45, 90, 9808 };
 
        // 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("Largest in given array is "
                           + Collections.max(list));
    }
}


Output

Largest in given array is 9808

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads