Given an unsorted array of integers numbers
, determine the length of the longest sequence where the integers appear consecutively, meaning the numbers follow one another without any gaps.
numbers: number[]
: An array of integersInput: numbers = [5,1,-4]Output: 1Explanation: No consecutive numbers exist, so the longest consecutive sequence is any single number, giving a length of 1
Input: numbers = [1,-1,-1,-4,-5]Output: 2Explanation: The consecutive sequences are [1], [-1], [-5, -4]. The longest is [-5, -4], with a length of 2
Input: numbers = [1,-1,0,8,11,10,9,9]Output: 4Explanation: The longest consecutive sequence is 8, 9, 10, 11, with a length of 4 and duplicates are ignored.
numbers.length
<= 100,000numbers[i]
<= 1,000,000,000Given an unsorted array of integers numbers
, determine the length of the longest sequence where the integers appear consecutively, meaning the numbers follow one another without any gaps.
numbers: number[]
: An array of integersInput: numbers = [5,1,-4]Output: 1Explanation: No consecutive numbers exist, so the longest consecutive sequence is any single number, giving a length of 1
Input: numbers = [1,-1,-1,-4,-5]Output: 2Explanation: The consecutive sequences are [1], [-1], [-5, -4]. The longest is [-5, -4], with a length of 2
Input: numbers = [1,-1,0,8,11,10,9,9]Output: 4Explanation: The longest consecutive sequence is 8, 9, 10, 11, with a length of 4 and duplicates are ignored.
numbers.length
<= 100,000numbers[i]
<= 1,000,000,000console.log()
语句将显示在此处。