Last Updated : 23 Jul, 2025
Try it on GfG Practice
Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.
Example Input/Output:
Approaches to Find the Largest Element in an ArrayInput: arr = { 1, 2, 3, 4, 5}
Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}
Output: 12Input: arr = { 12 }
Output: 12
Below are the 4 main approaches:
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.
Algorithm:
Example:
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()
{
// Initialize maximum element
int max = arr[0];
// Traversing and comparing max element
for (int i = 1; i < arr.length; i++)
// If current element is greater than max
if (arr[i] > max)
// Then update max element
max = arr[i];
return max;
}
public static void main(String[] args)
{
System.out.println(largest());
}
}
Explanation: In the above example, we traverse through each element of the array and check if the current element is greater than the max element so we update the max element and after reaching the end of the array we get the maximum array present in the array.
Complexity Analysis:
Best used for concise and parallelizable with other operations with the Time complexity of O(N).
Example:
Java
// Java Program to Find the Largest
// Element in Array using Java Stream
import java.util.Arrays;
public class Geeks
{
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();
System.out.println(max);
}
}
Complexity Analysis:
We can sort the array to get the largest element from the array. After sorting we can directly extract the last element as the largest element (Ascending order). We can use a predefined sort method defined in the util class.
Example:
Java
// Java Program to find the largest element
// in the array using sort function
import java.io.*;
import java.util.*;
class Geeks
{
public static void main(String[] args)
{
// Array created
int arr[] = {20, 10, 20, 4, 100};
// Sorting function using sort function
Arrays.sort(arr);
System.out.println(arr[arr.length - 1]);
}
}
Complexity Analysis:
Note: Instead of the Arrays.sort() method we can also use the user-defined sorting method.
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.
Example:
Java
// Java Program to find the maximum element
// in an Array using Collections.max() method
import java.util.*;
public class Geeks
{
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));
}
}
Complexity Analysis:
Approach
Time Complexity
Space Complexity
Use Case
Iterative
O(N)
O(1)
For general purpose
Java 8 Stream
O(N)
O(1)
For modern Java programs
Sorting
O(N log N)
O(1)
When array needs sorting too
Collections.max()
O(N)
O(N)
Working with ArrayLists
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4