WorkspaceProblem lab
ALGORITHM VISUALIZER

Longest Consecutive Sequence

Opening the interactive workspace…

Understanding Longest Consecutive Sequence

Return the length of the longest run of consecutive integer values, regardless of their positions in nums.

How it works

Only values with no predecessor can begin a consecutive run.

Build a set of distinct values. Skip any value whose predecessor is present. Walk forward from each run start and keep the longest length.

Try an example

Input: nums = [100, 4, 200, 1, 3, 2]

Output: 4

Only values with no predecessor can begin a consecutive run.

Constraints

  • 0–64 integers between -1,000,000 and 1,000,000.
  • Duplicate values do not lengthen a run.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity