Find the Largest Element in an Array
Return the maximum value in an integer array using a single pass.
Start by assuming the first element is the largest. Walk the rest of the array once; whenever you find a value greater than your current maximum, update the maximum. After a single pass the maximum holds the largest element. Initialize from the first element, not 0, so negative arrays work correctly.
Problem Statement
Given a non-empty array of integers, return its largest element. You should do this in a single linear pass without sorting.
For example, in [3, 7, 2, 9, 4] the largest element is 9. The array may
contain negative numbers, so [-5, -1, -9] has largest element -1.
Input: A non-empty array of integers arr.
Output: The largest value contained in arr.
Examples
Input: [3, 7, 2, 9, 4]
Output: 99 is greater than every other element in the array.
Input: [-5, -1, -9]
Output: -1Among the negatives, -1 is the closest to zero and therefore the largest.
Constraints
1 <= arr.lengthElements may be negative, zero, or positive
Think Before You Code
Reveal the questions to ask yourself first
- What should you initialize your running maximum to so negatives are handled?
- How many passes over the array do you really need?
- What is a sensible response if the array were empty?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Keep a running maximum and update it as you scan.
- Set
max = arr[0]— a real element, so it is always valid. - Loop
ifrom 1 toarr.length - 1:- If
arr[i] > max, setmax = arr[i].
- If
- Return
max.
Seeding max with the first element (rather than 0 or Integer.MIN_VALUE) makes
the code correct for arrays of any sign and avoids a magic sentinel.
Dry Run
Walk through the example step by step
Finding the largest in [3, 7, 2, 9, 4]:
start: max = arr[0] = 3
i | arr[i] | arr[i] > max? | max
--+--------+---------------+----
1 | 7 | yes | 7
2 | 2 | no | 7
3 | 9 | yes | 9
4 | 4 | no | 9
result: 9
Solution
Reveal the full Java solution
public class LargestElement {
public static int findLargest(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static void main(String[] args) {
System.out.println(findLargest(new int[]{3, 7, 2, 9, 4})); // 9
System.out.println(findLargest(new int[]{-5, -1, -9})); // -1
}
}
One comparison per element gives a single linear pass, which is optimal — you
must look at every element at least once to be sure of the maximum. Seeding from
arr[0] is what makes negative-only arrays work without a special sentinel value.
O(n) where n is the array lengthSpace: O(1)Common Mistakes
- Initializing max to 0, which returns 0 for an all-negative array.
- Starting the loop at index 0 (harmless but redundant) or at index 2 (skips an element).
Edge Cases to Test
- A single-element array returns that element.
- An all-negative array returns the value closest to zero.
- An array with duplicate maximums returns that maximum value.
Interview Follow-Ups
- How would you also return the index of the largest element?
- How would you find the largest and smallest elements in one pass?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →