LeetCode has a lot of great coding problems, but few are as satisfying as solving Longest Consecutive Sequence. It’s a fun mix of problem-solving, logic, and a little bit of speed thinking. If you’ve ever had to find a long streak of numbers in a list, this one’s for you!
TL;DR
The Longest Consecutive Sequence problem asks you to find the length of the longest streak of numbers that appear one after another in an unsorted list. You can’t sort the array—that would be too slow for big inputs. Instead, you use a set to find the start of sequences and track how long they go. The sweet spot is an O(n) time solution using hashsets.
What The Problem Is
Here’s the problem in simple words: You’re given an unsorted list of integers. Find the length of the longest consecutive elements sequence. The sequence elements have to be next to each other numerically, not positionally.
For example, if you get this list:
[100, 4, 200, 1, 3, 2]
The longest consecutive sequence is [1, 2, 3, 4], which has length 4.
Constraints
- You must do this in O(n) time.
- Think about how to avoid sorting.
Why Sorting Won’t Work
You might want to sort the array and then count how long sequences go. And yes, that works. But sorting takes O(n log n) time at least—too slow!
That’s why we need a smarter way. Enter: HashSet.
Using a Set for Speed
A set lets you look up values in O(1) time. It’s fast, reliable, and perfect for this job.
Here’s the step-by-step plan:
- Put all numbers in a set.
- Loop through each number in your list.
- For each number, check if it’s the start of a sequence.
- If yes, count how long the sequence goes.
- Keep track of the longest one.
How Do We Know It’s a Start?
A number is the start of a sequence if there’s no number before it in the set. So:
if (num - 1) not in set:
…then it’s a start. Once you find a start, keep checking num + 1, num + 2, etc., until the chain breaks.
Time to Code!
Here’s the magic formula in Python:
def longestConsecutive(nums):
num_set = set(nums)
longest = 0
for num in nums:
# Only start counting if it's the beginning of a sequence
if num - 1 not in num_set:
length = 1
while num + length in num_set:
length += 1
longest = max(longest, length)
return longest
Let’s Break This Down
Here’s what’s happening:
- We build a set of all numbers in the list.
- We assume each number could be the start of a sequence.
- If there’s no smaller neighbor (num – 1), that means it’s really the start.
- Then we look for all numbers in the streak: num + 1, num + 2, and so on.
- We update the longest one found.
It’s elegant, fast, and avoids any unnecessary checks. Perfect for interviews!
Example: Step-by-Step
Take this array: [100, 4, 200, 1, 3, 2]
- Put it in a set:
{1, 2, 3, 4, 100, 200} - Start with 100: 99 isn’t in the set, so it’s a start. Only 101? No. Length = 1
- Next, 4: 3 is in the set, so skip. It’s not a start.
- Then 200: 199 not in set → start. 201? No. Length = 1
- Next 1: 0 not in set → start! Check 2, 3, 4. Boom! Length = 4
Gotchas To Watch For
- Duplicates in the list? The set takes care of them!
- Empty list? The answer is 0.
- You don’t need to store the sequences—just the length.
More Fun Inputs
Try this one:
[9, 1, 0, -1, 8, 2, -2]
You should find the sequence [-2, -1, 0, 1, 2] → Length = 5
Complexity Check
Is it fast? Yep!
- Time: O(n), because each number is visited once
- Space: O(n), since we store the numbers in a set
This makes it one of the best solutions available for huge inputs.
What You’ll Learn By Solving It
- How to trade sorting for hashing
- How to identify sequence “starts” smartly
- Why sets can be a game-changing data structure
- How to write cleaner, tighter Python code
Similar Problems To Practice
Want more? Try these:
- Top K Frequent Elements
- Group Anagrams
- Valid Anagram
- Longest Substring Without Repeating Characters
Final Thoughts
The Longest Consecutive Sequence is like a secret test: Can you optimize? Can you avoid brute force? Can you think in sets, not loops?
Now that you’ve got it down, it’s a great time to strengthen your understanding of hashsets and sequence-detection techniques. Smart code is happy code!
Go out there and conquer that LeetCode board. You’ve got this!
