Java Program to Find Largest Element in an Array (original) (raw)
Last Updated : 09 Apr, 2025
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:
**Input: arr = { 1, 2, 3, 4, 5}
**Output: 5**Input: arr = { 10, 3, 5, 7, 2, 12}
**Output: 12**Input: arr = { 12 }
**Output: 12
Approaches to Find the Largest Element in an Array
Below are the 4 main approaches:
- Iterative Approach
- Java 8 Stream
- Sorting
- Using Collections.max()
1. Iterative Approach
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:
- We assume that the first element is the largest and initialize the first element as the largest number ( max = arr[0]).
- Then, we traverse through each element of the array and compare it with each element of the array one by one.
- If the current element is greater than the maximum, then we update the maximum ( max = arr[I]).
- When the loop is finished. We get the largest element and print it.
**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:
- **Time Complexity: O(N)
- **Space Complexity: O(1)
**2. Using Java 8 Stream (Used for Java 8+)
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:
- **Time Complexity: O(N)
- **Space Complexity: O(1)
**3. 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 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:
- **Time Complexity: O(N log(N) )
- **Space Complexity: O(1)
**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:
- **Time Complexity: O(N)
- **Space Complexity: O(N)
Approaches Comparision
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 |