Longest Consecutive Sequence
Medium Arrays & Hashing AI round
nums is a list of whole numbers in no particular order. It may be empty, and a number may appear more than once.
A run is a stretch of consecutive whole numbers, such as 4, 5, 6 and 7, that are all in nums, wherever they are in the list. A number that appears more than once counts once.
Return the length of the longest run, or 0 if nums is empty.
Example 1
Input: nums = [9,2,11,3,10,4,1,12,13]
Output: 5
9, 10, 11, 12 and 13 are all in the list, a run of 5. 1, 2, 3 and 4 make a run of 4.
Example 2
Input: nums = [5,-1,0,0,1,-2,7]
Output: 4
-2, -1, 0 and 1 make a run of 4, and the second 0 counts once. 5 and 7 are runs of 1.
Example 3
Input: nums = []
Output: 0
Constraints
0 ≤ nums.length ≤ 10⁵-10⁹ ≤ nums[i] ≤ 10⁹
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Arrays & Hashing
- Two SumAI round · Easy
- Majority ElementAI round · Easy
- First Missing PositiveHard
- Spiral MatrixMedium
- Set Matrix ZeroesMedium