Skip to content
BytePatterns

Longest Consecutive Run

MediumHash Tables#hash-set#counting~30m

Problem

Given an unsorted list of integers, find the length of the longest group of numbers that could be lined up as consecutive values with no gaps. The numbers do not have to be adjacent in the list, and duplicates count only once. Aim for a solution that does not sort the input.

Examples

Input:  nums = [9, 4, 2, 3, 1, 8]
Output: 4
Why:    1, 2, 3 and 4 form an unbroken chain
Input:  nums = [5, 5, 5]
Output: 1
Why:    duplicates add nothing, the chain is just the value 5
Input:  nums = []
Output: 0
Why:    edge case, there is no chain to measure

Hints

0 / 3

Stuck on the idea rather than the code? Hash Table Basics covers it.