0
0
JavaProgramBeginner · 2 min read

Java Program to Find Largest Element in Array

To find the largest element in an array in Java, use a loop to compare each element with a variable holding the current largest value, like int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) max = arr[i]; }.
📋

Examples

Input[3, 5, 1, 9, 2]
Output9
Input[10, 10, 10]
Output10
Input[-5, -1, -10, -3]
Output-1
🧠

How to Think About It

To find the largest number, start by assuming the first element is the largest. Then, check each other element one by one. If you find a bigger number, update your largest number. At the end, the largest number you have is the biggest in the array.
📐

Algorithm

1
Start with the first element as the largest number.
2
Go through each element in the array from the second to the last.
3
Compare the current element with the largest number.
4
If the current element is bigger, update the largest number.
5
After checking all elements, return the largest number.
💻

Code

java
public class LargestElement {
    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 9, 2};
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println("Largest element is: " + max);
    }
}
Output
Largest element is: 9
🔍

Dry Run

Let's trace the array [3, 5, 1, 9, 2] through the code

1

Initialize max

max = 3 (first element)

2

Compare with second element

arr[1] = 5 > max (3), so max = 5

3

Compare with third element

arr[2] = 1 <= max (5), max stays 5

4

Compare with fourth element

arr[3] = 9 > max (5), so max = 9

5

Compare with fifth element

arr[4] = 2 <= max (9), max stays 9

6

End of loop

max = 9 is the largest element

IterationCurrent Elementmax Value
Start33
155
215
399
429
💡

Why This Works

Step 1: Start with first element

We assume the first element is the largest to have a starting point for comparison.

Step 2: Compare each element

We check each element with the current largest using if (arr[i] > max) to find if there's a bigger number.

Step 3: Update largest value

When a bigger number is found, we update max to hold this new largest value.

Step 4: Result after loop

After checking all elements, max holds the largest element in the array.

🔄

Alternative Approaches

Using Java Streams
java
import java.util.Arrays;
public class LargestElementStream {
    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 9, 2};
        int max = Arrays.stream(arr).max().orElse(Integer.MIN_VALUE);
        System.out.println("Largest element is: " + max);
    }
}
This uses Java 8+ streams for a concise solution but may be less clear for beginners.
Sorting the array
java
import java.util.Arrays;
public class LargestElementSort {
    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 9, 2};
        Arrays.sort(arr);
        System.out.println("Largest element is: " + arr[arr.length - 1]);
    }
}
Sorting is simple but less efficient because it sorts the whole array just to find the largest element.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once, so the time grows linearly with the array size, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The loop method is fastest and simplest. Sorting takes O(n log n) time, and streams add overhead but are concise.

ApproachTimeSpaceBest For
Loop through arrayO(n)O(1)Simple and efficient for all arrays
Java StreamsO(n)O(1)Concise code, requires Java 8+
Sorting arrayO(n log n)O(1)When array needs sorting anyway
💡
Always initialize your largest value with the first element of the array to avoid errors.
⚠️
A common mistake is starting the largest value at zero, which fails if all array elements are negative.