Second Largest Value (Set 13)

Easy

Given an array of at least two integers nums, return the second largest distinct value. If every value is equal, return that value.

Second Largest Value (Set 13) — the data. This set runs 4 cases: nums holds arrays of 48–48 items, with values from -20 to 25. The hidden cases are the largest of the set.

Example 1

secondLargestValue()
Input
nums = [-4, 10, 2, -12, -10, -2, -9, -6, 11, 8, -15, -1, 22, 17, -15, -2, -2, 0, -4, 0, 2, 7, 3, 2, 0, 20, -15, 13, -14, 21, -15, -7, -19, -18, 25, 8, 9, 16, 3, -18, 6, 12, 2, 3, 21, -18, 22, 23]
Output
23

Why

`secondLargestValue` returns `23` for this input.

Example 2

secondLargestValue()
Input
nums = [6, -16, 18, 22, -4, -5, 18, -15, 1, 0, 5, 16, -19, -4, 20, 5, -5, 11, 18, -7, -18, 11, 13, 1, -12, -3, 1, -1, -2, 19, 22, -7, -13, -4, 10, 20, 0, 5, 10, -10, 18, -15, 8, 9, 2, -19, 1, 11]
Output
20

Why

`secondLargestValue` returns `20` for this input.
Constraints
2 <= nums.length <= 10^5
-10^5 <= nums[i] <= 10^5
Every case in this set must pass: the first two are visible on Run, the last two unlock on Submit.

Topics

Arrays

Implement secondLargestValue in the editor and return the answer — printing it is never graded.