How to Find Second Largest Element in Array in Java
The Challenge
Find the second largest element in an array!
Solutions
Method 1: Using Two Variables
int[] numbers = {5, 2, 8, 1, 9, 3};
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : numbers) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
System.out.println("Second Largest: " + secondLargest);
// Output: Second Largest: 8Method 2: Using Sorting
import java.util.Arrays;
int[] numbers = {5, 2, 8, 1, 9, 3};
Arrays.sort(numbers);
int secondLargest = numbers[numbers.length - 2];
System.out.println("Second Largest: " + secondLargest);
// Output: Second Largest: 8Did You Know?
The two-variable method is more efficient (O(n)) than sorting (O(n log n)) and works in a single pass!
Watch Out!
Handle duplicate values correctly! The condition num != largest ensures we don't count the same value twice.
Real-World Use Cases
- Gaming: Find runner-up score
- Competitions: Determine second place
- Analytics: Find second-best performance
›Short versionOpen if you only have a minute.
- One pass can track the largest and a distinct second.
- When a new largest arrives, the old largest becomes the second.
- A duplicate of the largest must not become the second.
- Sorting and reading length - 2 mutates the array and treats a repeated maximum as the second value.
Second largest
Find the largest value that is strictly below the maximum.
Working approach
- Need at least two distinct values. Otherwise there is no answer.
- Update both trackers when a new maximum is seen.
- Update only the second when the value sits strictly between the two.
Walk the sample
- Values 5, 9, 9, 3, 8
- 9 becomes largest and 5 becomes second
- The extra 9 is ignored
- 8 replaces 5
- The result is 8
The trap
Integer.MIN_VALUE as a sentinel cannot tell "no second value" from "the second value is Integer.MIN_VALUE". Sorting then taking the second index also returns the maximum twice when that maximum is duplicated.
static int secondLargest(int[] values) {
if (values.length < 2) {
throw new IllegalArgumentException("need two elements");
}
int largest = values[0];
Integer second = null;
for (int i = 1; i < values.length; i++) {
int value = values[i];
if (value > largest) {
second = largest;
largest = value;
} else if (value < largest && (second == null || value > second)) {
second = value;
}
}
if (second == null) {
throw new IllegalArgumentException("no distinct second");
}
return second;
}For {9, 9, 9}, what is the second largest distinct value?
There is not one. Every element equals the maximum.
numbers[numbers.length - 2] after sorting is 9, the maximum again. The condition value != largest is what keeps a duplicate from occupying the second slot, and the method still has to say when that slot was never filled.
The one-pass scan is O(n) time and O(1) space. Sorting is O(n log n) and mutates.
Easy to get wrong
- Returning the duplicated maximum as if it were second.
- Using Integer.MIN_VALUE as the second value without a flag that says it was seen.