Java Program to Find Second Largest Element in Array
if conditions, for example: if (arr[i] > largest) { secondLargest = largest; largest = arr[i]; } else if (arr[i] > secondLargest && arr[i] != largest) { secondLargest = arr[i]; }.Examples
How to Think About It
Algorithm
Code
public class SecondLargest { public static void main(String[] args) { int[] arr = {12, 35, 1, 10, 34, 1}; int largest = Integer.MIN_VALUE; int secondLargest = Integer.MIN_VALUE; for (int num : arr) { if (num > largest) { secondLargest = largest; largest = num; } else if (num > secondLargest && num != largest) { secondLargest = num; } } if (secondLargest == Integer.MIN_VALUE) { System.out.println("No second largest element"); } else { System.out.println("Second largest element is " + secondLargest); } } }
Dry Run
Let's trace the array [12, 35, 1, 10, 34, 1] through the code
Initialize
largest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE
Check 12
12 > largest(-2147483648), so secondLargest = largest(-2147483648), largest = 12
Check 35
35 > largest(12), so secondLargest = 12, largest = 35
Check 1
1 > secondLargest(12)? No, no change
Check 10
10 > secondLargest(12)? No, no change
Check 34
34 > secondLargest(12) and 34 != largest(35), so secondLargest = 34
Check 1
1 > secondLargest(34)? No, no change
Result
secondLargest = 34
| Iteration | Current Number | Largest | Second Largest |
|---|---|---|---|
| 1 | 12 | 12 | -2147483648 |
| 2 | 35 | 35 | 12 |
| 3 | 1 | 35 | 12 |
| 4 | 10 | 35 | 12 |
| 5 | 34 | 35 | 34 |
| 6 | 1 | 35 | 34 |
Why This Works
Step 1: Track largest and second largest
We keep two variables to remember the biggest and the second biggest numbers found so far.
Step 2: Update when bigger found
If a number is bigger than the largest, it becomes the new largest and the old largest becomes second largest.
Step 3: Update second largest carefully
If a number is not bigger than largest but bigger than second largest and not equal to largest, update second largest.
Step 4: Check if second largest exists
If second largest never changed from initial minimum, it means no second largest element exists.
Alternative Approaches
import java.util.Arrays; public class SecondLargestSort { public static void main(String[] args) { int[] arr = {12, 35, 1, 10, 34, 1}; Arrays.sort(arr); int n = arr.length; int largest = arr[n-1]; int secondLargest = Integer.MIN_VALUE; for (int i = n-2; i >= 0; i--) { if (arr[i] != largest) { secondLargest = arr[i]; break; } } if (secondLargest == Integer.MIN_VALUE) { System.out.println("No second largest element"); } else { System.out.println("Second largest element is " + secondLargest); } } }
import java.util.TreeSet; public class SecondLargestTreeSet { public static void main(String[] args) { int[] arr = {12, 35, 1, 10, 34, 1}; TreeSet<Integer> set = new TreeSet<>(); for (int num : arr) { set.add(num); } if (set.size() < 2) { System.out.println("No second largest element"); } else { set.pollLast(); // remove largest System.out.println("Second largest element is " + set.last()); } } }
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through the array once, so it takes linear time proportional to the number of elements, O(n).
Space Complexity
Only a few variables are used to track largest and second largest, so space is constant, O(1).
Which Approach is Fastest?
The single pass method is fastest with O(n) time and O(1) space, while sorting or TreeSet methods are slower due to O(n log n) time.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Single pass scan | O(n) | O(1) | Fastest and simplest for large arrays |
| Sorting array | O(n log n) | O(1) | Simple but slower, useful if array needs sorting anyway |
| TreeSet method | O(n log n) | O(n) | Handles duplicates easily, but uses extra memory |