ASAPUtils Logo ASAPUtils
Week 1

Longest Consecutive Sequence

Longest Consecutive Sequence in O(n) without sorting, with C++ and JavaScript solutions, a step-by-step visualizer, and the amortised argument for why a nested while loop is still linear.

The problem

Given an unsorted integer array, return the length of the longest run of consecutive integers present in it. The run does not need to appear contiguously in the array. You must solve it in O(n) time.

nums = [100, 4, 200, 1, 3, 2] -> 4 (the run 1, 2, 3, 4)

Related Problems