Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Example 3:
Input: nums = [1,0,1,2]
Output: 3
Constraints:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
Solutions
Solution 1: Hash Table
We can use a hash table \(\textit{s}\) to store all the elements in the array, a variable \(\textit{ans}\) to record the length of the longest consecutive sequence, and a hash table \(\textit{d}\) to record the length of the consecutive sequence each element \(x\) belongs to.
Next, we iterate through each element \(x\) in the array, using a temporary variable \(y\) to record the maximum value of the current consecutive sequence, initially \(y = x\). Then, we continuously try to match \(y+1, y+2, y+3, \dots\) until we can no longer match. During this process, we remove the matched elements from the hash table \(\textit{s}\). The length of the consecutive sequence that the current element \(x\) belongs to is \(d[x] = d[y] + y - x\), and then we update the answer \(\textit{ans} = \max(\textit{ans}, d[x])\).
After the iteration, we return the answer \(\textit{ans}\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(\textit{nums}\).
Similar to Solution 1, we use a hash table \(\textit{s}\) to store all the elements in the array and a variable \(\textit{ans}\) to record the length of the longest consecutive sequence. However, we no longer use a hash table \(\textit{d}\) to record the length of the consecutive sequence each element \(x\) belongs to. During the iteration, we skip elements where \(x-1\) is also in the hash table \(\textit{s}\). If \(x-1\) is in the hash table \(\textit{s}\), then \(x\) is definitely not the start of a consecutive sequence, so we can directly skip \(x\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(\textit{nums}\).