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
Longest Consecutive Sequence in [100, 4, 200, 1, 3, 2] O(1) Hash Set Lookups • Selective Head Trigger (x - 1 missing) • O(N) Total Time 1. Input Array & O(1) Hash Set nums = [100, 4, 200, 1, 3, 2] 100 4 200 1 3 2 Hash Set Loaded. Instant lookup enabled. Goal: Find longest contiguous chain without full sort. 2. The Core Rule: Is (x - 1) in Set? If (x - 1) exists in set ⇒ SKIP (interior element) If (x - 1) absent ⇒ START counting run! (Sequence Head) Why inspect '3' skips: 2 is in set, so 3 is not a head! 3. Tracing Elements & Building Sequence Islands Inspect 100 99 absent? Yes (Head!) Run: [100] Length: 1 Inspect 4 3 exists in set? Yes (Interior) SKIPPED No redundant work Inspect 200 199 absent? Yes (Head!) Run: [200] Length: 1 Inspect 1 (Sequence Head!) 0 absent? Yes. Count upwards: 1, 2, 3, 4 1 2 3 4 Max Longest Run Length = 4
Longest consecutive run in [100, 4, 200, 1, 3, 2] overview diagram
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.
Why Unsorted Sequences Break Our Intuition for Counting Goal: Find longest consecutive run in nums = [100, 4, 200, 1, 3, 2] without paying the full O(N log N) sorting tax 1. Raw Array (Scrambled) Scattered in memory. Eyes want to line them up. 100 4 200 1 3 2 2. Full Sorting Trap: O(N log N) Heavy comparison cost just to spot adjacent neighbors. 1 2 3 4 100 200 sort? 3. Optimal O(N) Strategy: Hash Set & Sequence Anchor Detection Instead of sorting, drop everything into a Hash Set. Only start counting when a number has NO left neighbor (num - 1 is missing)! Hash Set (O(1) Lookups) 100 4 200 1 (Anchor!) 3 2 Check: Is (num - 1) in set? • 1-1=0 (No) ⇒ Start sequence! Instant Chain Building (Length = 4) 1 Start 2 Exists 3 Exists 4 Exists Result: Max Consecutive Length = 4 (Visited in O(N) total time!)
Why unsorted sequences break our intuition for counting diagram
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]
Longest Consecutive Sequence — The Set Lookup Model Target: nums = [100, 4, 200, 1, 3, 2] O(N) Time via Hash Set + Selective Trigger (x - 1 missing) 1. Unsorted Input Array 100 4 200 1 3 2 ... O(N) Insert 2. Hash Set (O(1) Lookups) 100 4 200 1 3 2 Iterate & Check (x - 1) 3. The Selective Trigger Rule Is (x - 1) in the Set? YES → Skip (Interior Element) | NO → Start Sequence! Step-by-Step Trace: Why we only count at sequence heads 4 Check x - 1 = 3: Found in set! Result: SKIPPED (not a sequence start, 3 exists) 100 Check x - 1 = 99: ABSENT Result: START COUNT! Run length = 1 [100] 1 Check x - 1 = 0: ABSENT Result: START COUNT! Streak expansion loop: 1 2 3 4 5? X Final Answer: Longest Consecutive Run = 4 ([1, 2, 3, 4])
The Set Lookup Model for Unsorted Sequences diagram
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.
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 .
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 .
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.
Tracing Set Lookup: Why inspecting '3' is skipped Hash Set State O(1) 100 4 200 1 3 2 nums = [100, 4, 200, 1, 3, 2] Inspect Number: 3 Check Predecessor: Is (3 - 1) = 2 in Set? Yes! '2' Exists in Set Predecessor found ➔ Not a sequence head SKIP IT! No inner loop Key Concept: Preventing Redundant Work • Because 2 is already in the hash set, 3 is guaranteed to be part of a larger sequence starting at 1. • Checking 3 independently would duplicate the counting work already done (or about to be done) for 1. Result: Each consecutive sequence is counted exactly once from its lowest boundary (head), ensuring O(n) overall time.
Tracing the Set Lookup Model on Unsorted Data diagram
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.

Keep learning