Skip to main content

Longest Consecutive Sequence

Learn Longest Consecutive Sequence (Hashing, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Longest Run of Consecutive Integers Given an unsorted array, find the length of the longest run of consecutive integers (e.g. [100, 4, 200, 1, 3, 2] → the run 1,2,3,4 has length 4). Sorting first gives O(n log n); a HashSet gives O(n) by only starting a count from numbers that are the BEGINNING of a run.

More lessons in Hashing