intermediate7 min read·Updated September 17, 2026
Longest Consecutive Sequence Explained: Tracing [100, 4, 200, 1, 3, 2]
Master the O(n) hash set mental model for finding the longest consecutive sequence. Walk through example [100, 4, 200, 1, 3, 2], edge cases, and time complexity.
By Learnisim AI·Published September 17, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Hash sets and hash maps
- Basic array iteration
- Big-O time complexity
Why
Why unsorted sequences break our intuition for counting
Imagine you are handed a scrambled set of numbers like our given array and asked to find the length of the longest consecutive sequence where numbers follow each other in counting order, such as . Your eyes naturally want to line them up in order, but what happens when the array grows from six elements to six million? Sorting the entire collection takes time, which feels unnecessarily heavy just to spot adjacent numerical neighbors hiding across distant memory addresses. Without a deliberate strategy, you end up repeatedly scanning the array or losing track of which numbers you have already counted in your runs. We need a way to spot these chains instantly without paying the full sorting tax, setting up the exact problem that the longest consecutive sequence technique solves.
Model
The Set Lookup Model for Unsorted Sequences
Instead of sorting nums = in time, we can visualize the problem as building islands of consecutive integers. We first insert every element of our array into a hash set to allow lookups. The core mental model relies on a selective trigger: we only start counting the length of a sequence if the number immediately preceding it () is absent from the set. For instance, when looking at 4, we check if 3 exists; since it does, 4 is skipped because it is an interior element, not a sequence head. But when we encounter 1, we check if 0 is in the set—it is absent, meaning 1 is the official starting post of a valid contiguous run.
Try this: nums = [100, 4, 200, 1, 3, 2]
Worked example
Tracing the Set Lookup Model on Unsorted Data
Now let us trace our locked example array using the set lookup model we established. We will track the exact state of our hash set and how our conditional check prevents redundant inner loops.
Given:
loaded into a hash set for lookups.
loaded into a hash set for lookups.
Steps:
1. Insert all elements into the hash set: . Initialize .
2. Inspect 100: Is 99 in the set? No. Since is absent, 100 is a sequence head. We count upwards: 100 is present, 101 is missing. Length of this run is 1. Update .
4. Inspect 200: Is 199 in the set? No. 200 is a sequence head. Count upwards: 200 is present, 201 is missing. Length is 1. remains 1.
Update .
1. Insert all elements into the hash set: . Initialize .
2. Inspect 100: Is 99 in the set? No. Since is absent, 100 is a sequence head. We count upwards: 100 is present, 101 is missing. Length of this run is 1. Update .
3.Inspect 4: Is 3 in the set? Yes (3 is present). Therefore, 4 is part of an ongoing sequence and not a head. We skip it entirely to avoid redundant counting.
4. Inspect 200: Is 199 in the set? No. 200 is a sequence head. Count upwards: 200 is present, 201 is missing. Length is 1. remains 1.
5.Inspect 1: Is 0 in the set? No. 1 is a sequence head! We initiate our counting loop:
•Check 1: present. Length = 1.
•Check 2: present. Length = 2.
•Check 3: present. Length = 3.
•Check 4: present. Length = 4.
•Check 5: missing. Stop loop.
Update .
6.Inspect 3 and 2: Both have predecessors in the set (2 and 1 respectively), so our algorithm skips both instantly.
Result:
The longest consecutive sequence length found is 4, corresponding to the run .
The longest consecutive sequence length found is 4, corresponding to the run .
Try this: Given the intermediate state where the set is {100, 4, 200, 1, 3, 2} and nums = [100, 4, 200, 1, 3, 2], explain why inspecting the number 3 immediately terminates its check without starting an inner counting loop.
Practice
Practice Sequence Counting on a Modified Array
Now that you have traced the set lookup algorithm on the original locked example, it is time to test your mental model on a slightly different input. Consider an array that introduces negative numbers and a duplicate value:
nums = [-1, 0, 1, 0, 3, 2]. Following the exact mechanics you learned—converting the array to a hash set and checking whether x - 1 exists before starting a forward count—try to determine how the algorithm evaluates this new set. Remember that duplicates like 0 will share the same set membership, meaning checking x - 1 for each will behave cleanly without extra sorting overhead.Try this: Given nums = [-1, 0, 1, 0, 3, 2]:
1.Insert all elements into a hash set.
2.Identify which elements are valid sequence heads (i.e., x - 1 is absent).
3.What is the length of the longest consecutive sequence found?
Apply
Transferring the Set-Head Pattern to Longest Consecutive Subarray Sum
Now that you have mastered the hash set pattern for finding the longest consecutive run in , you can transfer this structural thinking to a related array puzzle. Suppose you are asked to find not just the longest sequence of consecutive integers, but the maximum sum of any contiguous subsegment whose elements form a consecutive sequence. The core pattern remains identical: instead of sorting the whole dataset in time, we use a membership structure to locate sequence boundaries instantly, ensuring we only scan forward when we stand at a valid sequence head.
FAQ
How does the algorithm handle the example [100, 4, 200, 1, 3, 2]?
It first loads all elements into a hash set. Then it iterates through the set, identifying sequence 'heads' (numbers that do not have a predecessor, like 1, 100, and 200). From each head, it counts upwards until a number is missing, finding that the sequence starting at 1 yields a maximum length of 4.
Why do we only start counting sequences from sequence heads?
Checking every number as a potential start would lead to O(n²) time complexity. By only initiating a count when
num - 1 is absent from the set, we guarantee that each sequence is only traversed once from its absolute beginning, achieving O(n) time.What is the time and space complexity of this approach?
Both time and space complexity are O(n). Inserting elements into the hash set takes O(n) time and space, and the subsequent linear scan visits each number at most twice (once as a head check, and at most once during a consecutive sequence count).
How does the algorithm handle duplicate numbers in the array?
Using a hash set automatically deduplicates the input array. Duplicates have no effect on the sequence logic or correctness since identical numbers map to the same set entry.