Finding the largest element in an array is a fundamental operation in Java programming. It is useful across many real-world applications, including statistical calculations and algorithmic problems.
An array of numbers (integers or floats), the task is to find and print the largest element in the array.
Approach 1: Using a for Loop (Basic Method)
This is the most common way to find the largest element.
// Java Program to Find the Largest // Element of an Array public class LargestElement { public static void main(String[] args) { int[] arr = {10, 45, 2, 67, 34}; // Assume first element is the largest int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { // Update max if current is greater max = arr[i]; } } System.out.println("Largest Element: " + max); } }
Output:
Largest Element: 67
Approach 2: Using for-each Loop
More concise and readable with Java’s enhanced loop.
// Java Program to Find the Largest // Element of an Array public class LargestElement { public static void main(String[] args) { int[] arr = {3, 22, 11, 56, 19}; int max = arr[0]; for (int num : arr) { if (num > max) { max = num; } } System.out.println("Largest Element: " + max); } }
Approach 3: Taking Input from User Using Scanner
Let the user define the array size and elements at runtime.
// Java Program to Find the Largest // Element of an Array import java.util.Scanner; public class LargestElement { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter Number of Elements: "); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { System.out.print("Enter " + (i+1) + " Element: "); arr[i] = sc.nextInt(); } int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) { max = arr[i]; } } System.out.println("Largest Element: " + max); sc.close(); } }
Approach 4: Using Java 8 Streams (Java 8+)
Java Stream API allows for a more functional approach.
// Java Program to Find the Largest // Element of an Array import java.util.Arrays; public class LargestElement { public static void main(String[] args) { int[] arr = {12, 99, 34, 23, 87}; // or use .orElse(Integer.MIN_VALUE) int max = Arrays.stream(arr).max().orElseThrow(); System.out.println("Largest Element: " + max); } }
Approach 5: Sorting the Array
Not the most efficient way if you only want the max, but still valid.
// Java Program to Find the Largest // Element of an Array import java.util.Arrays; public class LargestElement { public static void main(String[] args) { int[] arr = {5, 9, 1, 12, 6}; Arrays.sort(arr); int max = arr[arr.length - 1]; System.out.println("Largest Element: " + max); } }
Note:
- Time complexity is O(n log n) due to sorting, which is slower than a single pass (O(n)).
Approach 6: Using Collections (If Array is Converted to List)
Useful for arrays of wrapper types like Integer[]
.
// Java Program to Find the Largest // Element of an Array import java.util.Collections; import java.util.Arrays; import java.util.List; public class LargestElement { public static void main(String[] args) { Integer[] arr = {18, 42, 7, 26, 91}; List<Integer> list = Arrays.asList(arr); int max = Collections.max(list); System.out.println("Largest Element: " + max); } }
Edge Case Handling
1. Empty Array
int[] arr = {};
// Should handle or throw exception
2. Array with Negative Numbers
int[] arr = {-4, -10, -2, -11};
// Output should still be -2 (largest)
3. Single Element
int[] arr = {42};
// Output: 42
Always validate input arrays before processing!