Math Fundamentals18 sections · 814 units
Open in Course

Problem - Longest Consecutive

LeetCode 128

Given an unsorted array of integers nums\text{nums}, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n)O(n) time.

For example, given [100,4,200,1,3,2][100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1,2,3,4][1, 2, 3, 4], so the answer is 44.

This problem tests your ability to use sets cleverly. Sorting would take O(nlogn)O(n \log n), so you need a different approach.