0
0
JavaProgramBeginner · 2 min read

Java Program to Find Largest Element in Array

To find the largest element in an array in Java, you can use a loop to compare each element and keep track of the maximum value using code 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, -2, -9, -1]
Output-1
🧠

How to Think About It

To find the largest number in an array, start by assuming the first number is the largest. Then, look at each number one by one. If you find a number bigger than your current largest, remember that new number instead. After checking all numbers, the one you remembered last is the largest.
📐

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 found so far.
4
If the current element is bigger, update the largest number.
5
After checking all elements, return the largest number.
💻

Code

java
public class LargestInArray {
    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 to find the largest element.

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: Initialize max

We start by assuming the first element is the largest using int max = arr[0];.

Step 2: Compare each element

We check each element with if (arr[i] > max) to find if there is a bigger number.

Step 3: Update max

If a bigger number is found, we update max to that number to keep track of the largest.

Step 4: Result

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

🔄

Alternative Approaches

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

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 simple loop is fastest and uses least memory. Sorting is slower (O(n log n)) and streams add overhead but are concise.

ApproachTimeSpaceBest For
Simple loopO(n)O(1)Fastest and simplest 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 the largest value with the first element of the array to avoid errors.
⚠️
Beginners often forget to start the loop from the second element, causing incorrect comparisons.