Introduction
Finding the second largest element in an array means identifying the second biggest distinct value present in the array.
Given an array of integers arr[] of size n , the task is to find the second largest distinct element in the array.
Note:
If the second largest element does not exist, return: -1
Example
Input: arr[] = [7, 12, 4, 9, 15, 10]
Output: 12
Explanation:
Largest element = 15
Second largest element = 12
Input: arr[] = [5, 5, 5]
Output: -1
Explanation:
All elements are same
No second largest element exists
Constraints
- 1 <= n <= 10^5
- -10^9 <= arr[i] <= 10^9
Approach 1 : Brute Force (Using Sorting)
Explanation
The simplest way to solve this problem is:
- Sort the array
- Traverse from the end
- Find the first element smaller than the largest element
This approach is easy to understand but sorting increases time complexity.
Steps
- Sort the array in ascending order.
- Store the largest element.
- Traverse from the second last index.
- Find the first element different from the largest element.
- Return that value as the second largest element.
Dry Run:
Input Array: [12, 35, 1, 10, 34, 1]
After Sorting: [1, 1, 10, 12, 34, 35]
Largest Element: 35
Traverse from right to left:
34 is smaller than 35
Therefore, Second Largest = 34
Final Result: 34
Brute Force Code
Time Complexity: O(n log n)Explanation:
Sorting the array takes O(n log n) time.
Traversing from the end takes O(n) time.
Space Complexity: O(1)
Explanation:
No extra space is used apart from a few variables.Approach 2 : Optimized Solution (Single Traversal)
Explanation
Instead of sorting the array, we can find both the largest and second largest elements in a single traversal.
We maintain:
- largest element
- second largest element
and update them whenever required.
This avoids unnecessary sorting operations and improves efficiency.
Steps
- Initialize:
- largest = -1
- secondLargest = -1
- Traverse the array.
- If current element is greater than largest:
- update secondLargest = largest
- update largest = current element
-
Else if current element:
- is smaller than largest
- and greater than secondLargest
update secondLargest.
- Return secondLargest.
Dry Run:
Input Array: [12, 35, 1, 10, 34, 1]Initially: Largest = -1 Second Largest = -1
Traverse 12: 12 is greater than Largest Update: Largest = 12 Second Largest = -1
Traverse 35: 35 is greater than Largest (12) Update: Largest = 35 Second Largest = 12
Traverse 1: 1 is smaller than both values No update required
Traverse 10: 10 is greater than Second Largest (-1) Update Second Largest = 10
Traverse 34: 34 is smaller than Largest (35) 34 is greater than Second Largest (10) Update Second Largest = 34
Traverse 1: No update required Final Result: Second Largest = 34
Optimized Code
Complexity Analysis
Time Complexity: O(n)Explanation: The array is traversed only once. Each element is processed exactly one time.
Space Complexity: O(1) Explanation: Only two extra variables are used: largest and secondLargest. No additional data structures are required.
Edge Cases
- Array contains all same elements
- Array contains only one element
- Array contains negative numbers
- Duplicate largest elements are present
Why This Problem is Important
This problem builds the foundation for:
- Optimization-based problems
- Greedy techniques
- Heap problems
- Top K element problems
- Efficient traversal logic
Understanding how to track multiple values efficiently is important in many advanced DSA problems.
Real-World Applications
Finding the second largest element is used in:
- Ranking systems
- Sports leaderboards
- Performance analytics
- Competitive scoring systems
- Statistical analysis
Common Mistakes
- Forgetting duplicate elements
- Incorrect initialization
- Updating second largest incorrectly
- Sorting unnecessarily in optimized solution
Interview Tips
Interviewers often expect:
- an O(n) traversal solution
- handling duplicate values properly
- constant space optimization
Always explain why the single traversal approach is better than sorting.
Related Questions
- Find Largest Element
- Top K Frequent Elements
- Kth Largest Element
- Third Largest Element
- Leaders in an Array
Final Takeaway
Finding the second largest element in an array is an important beginner-level DSA problem that teaches efficient traversal, conditional updates, and optimization techniques. Understanding this problem helps build strong foundations for advanced array and heap-based interview problems.