../

Longest Consecutive Sequence

Given an unsorted array return the longest consecutive sequence. Run it in $O(n)$ time

Trivial (with no consideration of time)

  • Sort the array and traverse it while keeping track of consecutive elements

$O(n)$

  • The elements can be out of order. Convert the elements into a set. Iterate through them and check if the next element is present. If so increment the counter