Second Largest Distinct Element
Problem statement
Given an integer array nums, return its second largest distinct value. Repeated copies of a value count only once.
The array has at least two distinct values. Do not use built-in sorting functions. Use one traversal and constant extra space.
Function
secondLargestDistinct(nums: int[]) → intExamples
Example 1
nums = [10,5,20,8,20,15,7]return = 15The distinct values are 5, 7, 8, 10, 15, and 20, so 15 is second largest.
Example 2
nums = [-4,-1,-1,-7]return = -4The distinct values are -7, -4, and -1; the result is -4.
Constraints
2 <= nums.length <= 10^5.-10^9 <= nums[i] <= 10^9.- The array contains at least two distinct values.