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: 5Input: arr = { 10, 3, 5, 7, 2, 12}
Output: 12Input: arr = { 12 }
Output: 12
Approaches to Find the Largest Element in an Array
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 the array and compare each element with the current maximum value.
- If the current element is greater than the maximum, update the maximum (max = arr[i]).
- When the loop finishes, we get the largest element and print it.
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());
}
}
Output
100
Explanation: In the above example, We traverse the array and compare each element with the current maximum value. If a larger element is found, we update the maximum. After completing the traversal, the variable max contains the largest element in the array.
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).
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);
}
}
Output
100
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.
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]);
}
}
Output
100
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.
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));
}
}
Output
100
Approaches Comparison
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 |