Longest Unbroken Count

MediumHash Map≈ 30 min

You are given a list of integers nums in no particular order. Find the longest run of consecutive whole numbers, x, x + 1, x + 2, …, that all appear in the list, wherever they are in it. Return the length of that run.

A number that appears more than once still counts once. An empty list has no run, so the answer is 0.

Examples

text
nums = [100, 4, 200, 1, 3, 2]                   →  4
nums = [9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]      →  7
nums = [10, 30, 20]                             →  1
nums = []                                       →  0

In the first example 1, 2, 3, 4 are all there. In the second, 3, 4, 5, 6, 7, 8, 9 make a run of 7; -1, 0, 1 make one of 3.

Constraints

  • Between 0 and 20,000 numbers, each between -1,000,000,000 and 1,000,000,000.
  • Aim for O(n). Sorting, at O(n log n), passes too.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.

    3e\"}}],null]}]\n"])