Second Largest Element
The optimal single-pass solution maintains the largest and second-largest distinct values while scanning the array once. This avoids sorting, which would require O(n log n) time.
Time complexity: O(n).
Auxiliary space: O(1).
The comparison x != first ensures the second value is distinct.
Edge cases such as fewer than two distinct values should be handled explicitly.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience