Longest Consecutive Sequence - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

  • We are given an unsorted array of integers and want to find the length of the longest sequence of consecutive numbers.
  • First, we insert all numbers into a Set to allow quick lookups like β€œdoes x exist?” without sorting the array.
  • The key intuition is that we only need to start counting sequences from their beginnings. If a number (num) has an immediate predecessor (num - 1) in the set, then it cannot be the start of a new sequence β€” it belongs to a sequence that starts with a smaller number. By skipping all non-starting numbers, we only expand sequences from true starts, so each number is counted once as part of a valid expansion and skipped at most once.
  • We then iterate over each number num in the set:
    • If num - 1 exists in the set, we skip num, since it is not a sequence start and should be handled as part of the sequence that starts with a smaller number and includes it.
    • Otherwise, num is a sequence start:
      • We treat it as the beginning of a new consecutive sequence.
      • Starting from currentSeqNumber = num, we repeatedly check currentSeqNumber + 1, currentSeqNumber + 2, … while they exist in the set. For each such number found, we extend the current sequence and increase its length.
      • When the next consecutive number is not found in the set, the current sequence ends.
  • After finishing one sequence, we compare its length (currentStreak) with the best length seen so far (maxStreak):
    • If currentStreak > maxStreak, we update maxStreak to this new length.
    • Otherwise, we leave maxStreak unchanged.
    This ensures that maxStreak always stores the length of the longest consecutive sequence found so far.
  • Time Complexity: O(n)
    Each number is inserted into the set once, and for each sequence start we only walk forward through its consecutive elements once. Because we skip non-starting numbers, every element is part of exactly one sequence expansion.
  • Space Complexity: O(n)
    We store all numbers in the set, and the remaining variables use only constant extra space.

Time Complexity

Let n be the number of elements in the array.
All numbers are inserted into the Set, resulting in a total insertion time of O(n). During iteration, we only begin a sequence expansion when the current number num has no immediate predecessor (num - 1) in the set β€” meaning it’s a true sequence start. For each such start, we walk forward through all consecutive numbers (num + 1, num + 2, …) while they exist in the set, calculating the length of the current sequence. Each existence check takes amortized O(1) time. Each number is counted once as part of a valid expansion and skipped at most once. Hence, the total work across all numbers is linear in the size of the input array, resulting in an overall running time of O(n).

Space Complexity

Let n be the number of elements in the array.
We store all numbers in a Set to enable quick lookups, which requires O(n) space in total. Apart from this, only a few scalar variables are used, each contributing just O(1) additional space. Hence, the overall space complexity is O(n).

Visucodize editorial solution titled JS Solution for LeetCode's Longest Consecutive Sequence coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/longest-consecutive-sequence.