Intermediate Level
Implement the findSecondLargestDistinct method that returns the second largest distinct value from the array.
The input contains an integer array nums and its length size. Your task is to return the second largest distinct value in the array.
Duplicate values should be treated as one value. If there is no second distinct value, return -1. For example, in [5,1,9,9,3], the largest distinct value is 9 and the second largest distinct value is 5.
Keep track of the largest and second largest distinct values while scanning the array.
When a value is greater than the current largest, move the old largest to second largest. When a value is between largest and second largest, update only second largest. Ignore duplicates of the current largest or second largest.
Pseudocode:
function findSecondLargestDistinct(nums, size):
largest = none
second = none
for each value in nums:
if value == largest or value == second:
continue
if largest is none or value > largest:
second = largest
largest = value
else if second is none or value > second:
second = value
if second is none:
return -1
return second