Skip to content
BASA DSA

Longest Consecutive Sequence

MediumArrays & Hashing

Return the length of the longest run of consecutive integer values present in nums. Input order and duplicates do not affect the run. Aim for expected linear time.

Example 1

Input
nums = [9,2,4,3,2,1]
Output
4

Explanation

1,2,3,4 form a run of four; repeated 2 adds no length and 9 is isolated.

Example 2

Input
nums = []
Output
0

Explanation

There are no values from which to build a run.

Example 3

Input
nums = [-3,-1,-2,5]
Output
3

Explanation

-3,-2,-1 form a run of three even though the input is unsorted; 5 is separate.

Constraints

  • 0 <= len(nums) <= 100000
  • Values are signed 32-bit integers

Function contractlongestConsecutiveArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…