Longest Run of Consecutive Values
Problem statement
Given an integer array nums, find the largest number of consecutive integers (like 4, 5, 6, 7) that all appear somewhere in nums. Their positions in the array do not matter, and a value that appears several times counts once.
Return that number, or 0 if the array is empty.
Function
longestConsecutiveRun(nums: int[]) → intExamples
Example 1
nums = [10,4,12,11,5,13]return = 4The values 10, 11, 12 and 13 are all present, so they form a run of 4 consecutive integers (their order in the array does not matter). 4 and 5 form a shorter run of 2.
Example 2
nums = [7,7,8,8,6]return = 3Repeated values count once: 6, 7 and 8 form a run of 3.
Example 3
nums = [-3,100,0,-2,-1,50]return = 4-3, -2, -1 and 0 form a run of 4; 50 and 100 stand alone.
Constraints
0 <= nums.length <= 5000-10^9 <= nums[i] <= 10^9