# Leetcode 128 - Longest consecutive sequence

Note:

• Search in Set only takes O(1), so we can use it.
• How do we start counting the LCS? - Find the beginning first!
• When set has num-1, it means the cur is not the start. If we count now, we’ll have to repeat it again when we find the real start. So skip it!
• WHen there is no num-1, start counting it!
• What’s the time complexity?
• In the outer for-loop, you will iterate every num once.
• In the while loop, because we make sure that we only start from the head of LCS, we will also iterate every num in the current subsequence once.
• Every element will be iterated twice, so it’s O(2n), which is O(n).

Question:

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:

Code: