Menu
CoddyTech

Second Largest Number

You get a list of integers nums. Return its second largest distinct value: the largest value that is strictly smaller than the maximum. Values can repeat, so for [5, 5, 3] the answer is 3, not 5. The list always holds at least two different values.

Function

secondLargest(nums: integer-array) → integer
numsinteger-array
the list of integers, with at least two distinct values
Returnsinteger
the largest value that is smaller than the maximum

Constraints

  • 2 ≤ nums.length ≤ 5000
  • -109 ≤ nums[i] ≤ 109
  • nums contains at least two distinct values.

Examples

Input
nums = [4, 9, 2, 7, 9]
Output
7
Explanation
The maximum is 9. It appears twice, but a second copy of the maximum does not count, so the answer is the next value down, 7.

lock icon+15 hidden tests on Submit

challenge icon

Follow-up

Can you return the third largest distinct value in one pass, with three variables and no sorting?

Reset code
def secondLargest(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

nums = [4, 9, 2, 7, 9]

Expected

7