Longest Consecutive Sequence
You are given an unsorted array of integers nums. Return the length of the longest run of consecutive integers whose values all appear in nums, such as 4, 5, 6, 7.
The values don't need to be next to each other in the array, and a value that appears several times counts once. An empty array has a longest run of 0.
Your algorithm should run in O(n) time.
Constraints
0 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Examples
Example 1
Input: nums = [10,5,12,3,11,4,13] Output: 4
10, 11, 12, 13 is a run of 4 (3, 4, 5 is a run of 3). The values don't need to be next to each other in the array.
Example 2
Input: nums = [] Output: 0
An empty array has no run at all.
Example 3
Input: nums = [2,3,1,2] Output: 3
1, 2, 3: the repeated 2 counts once.
JavaScript you'll use: Map and Set
/**
* @param {number[]} nums
* @return {number}
*/
function longestConsecutive(nums) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.