Problem
You get an array of integers nums. The items are in no specific order. Return the length of the longest sequence of consecutive values that you can make from the items.
In a sequence of consecutive values, each value is 1 more than the value before it, for example 4, 5, 6. A value that appears more than one time counts only one time.
Your solution must run in O(n) time.
Example 1
- Input:
- nums = [8,-1,30,0,7,1,9,6]
- Output:
- 4
- Explanation:
- The values 6, 7, 8 and 9 make the longest sequence. The values -1, 0 and 1 make a shorter sequence.
Example 2
- Input:
- nums = [5,5,6,6,7]
- Output:
- 3
- Explanation:
- The repeated values count only one time. The sequence is 5, 6 and 7.
Example 3
- Input:
- nums = []
- Output:
- 0
Constraints
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Accounts Merge
Coding · Array · Breadth-First Search · Depth-First Search
- Minimize Malware Spread
Coding · Array · Breadth-First Search · Depth-First Search
- Number of Distinct Islands
Coding · Array · Breadth-First Search · Depth-First Search
- Number of Islands II
Coding · Array · Hash Table · Union-Find
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Amazon
Leetcode 128. Longest Consecutive Sequence
Reported Jan 15, 2026 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceVisa
Leetcode 128. Longest Consecutive Sequence
Reported Aug 1, 2026 · 1 source report
STAFF
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 8 records
Amazon
Longest Consecutive Sequence
Reported date not specified · Source window: 3mo
Source snapshot: Oct 20, 2026
Source frequency score: 61.2. This is not an interview probability.
View sourceAmazon
Longest Consecutive Sequence