Implement the longestConsecutiveSequenceLength method that returns the length of the longest consecutive number sequence.
The input contains an integer array nums and its length size. Your task is to return the length of the longest consecutive sequence of numbers.
The numbers do not need to be adjacent in the original array. A consecutive sequence means values like 1,2,3,4.
For example, in [100,4,200,1,3,2], the longest consecutive sequence is 1,2,3,4, so the answer is 4.
Store all numbers in a set so each value can be checked quickly.
Only start counting a sequence from a number that does not have a previous number before it. For example, start at 1 only if 0 is not present.
Then keep checking the next values one by one and update the longest length found.
Pseudocode:
function longestConsecutiveSequenceLength(nums, size):
values = set of nums
best = 0
for each number in values:
if number - 1 does not exist in values:
current = number
length = 1
while current + 1 exists in values:
current++
length++
best = maximum(best, length)
return best