Back to ArraysSecond Largest Element in an Array
Easy

Second Largest Element in an Array

EasyOptionalBasic Traversal

Given an array of distinct integers, find the second largest value in a single pass, without sorting.

Examples

Example 1

Input:
nums = [8,3,10,5,2]
Output:
8

Explanation: 10 is the largest; 8 is the next highest distinct value.

Example 2

Input:
nums = [1,2]
Output:
1

Example 3

Input:
nums = [12,35,1,10,34,1]
Output:
34

Constraints

  • 2 <= nums.length <= 10^5
  • All elements are distinct.
  • -10^9 <= nums[i] <= 10^9

Loading editor…

Code execution is coming soon.