Longest Sequence of Consecutive Numbers
ARRAY
HASH TABLE
Problem
Given an unsorted integer array nums
, determine the length of the longest sequence of consecutive elements.
Can you do it in O(n)
time?
Examples
longestSequence([10, 5, 12, 3, 55, 30, 4, 11, 2, 6]) // return 5 // Why? Longest sequence is [2, 3, 4, 5, 6] longestSequence([1, 3, 5, 7, 2, 11]) // return 3 // Why? Longest sequence is [1, 2, 3]
Loading...
Loading...Loading...