InferenceAI

Enter at least two characters to search.

Start free

Longest Consecutive Run

mediumArrays & Hashing25 min25 XP

Given an unsorted list of integers, return the length of the longest sequence of consecutive integers (such as 4, 5, 6, 7) that can be formed from its values. Order in the list does not matter, and duplicates count once.

Requirements

  • Run in O(n)O(n) time.

Examples

EXAMPLE WALKTHROUGHrun of four

A guided view of the published example, not a trace of your code.

INPUT[[100,4,200,1,3,2]]
TASKlongest_consecutive_run()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [100,4,200,1,3,2]

Output: 4

Explanation: run of four

Input: nums = [0,3,7,2,5,8,4,6,0,1]

Output: 9

Explanation: run of nine

Input: nums = []

Output: 0

Explanation: empty

Hint 1

Put the values in a set.

Hint 2

Only start counting from a value x when x - 1 is not in the set.

Topics

DSAArrays & HashingPython