← Practice · Hash map

Longest Consecutive Sequence Medium

Given an unsorted array of integers, return the length of the longest run of consecutive values (like 3,4,5,6). The run need not be contiguous in the array. Must run in O(n) time.

Examples

  • [100,4,200,1,3,2] -> 4 (1,2,3,4)
  • [0,3,7,2,5,8,4,6,0,1] -> 9

Define a function named longestConsecutive in JavaScript.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests