Given an unsorted list of integers, return the length of the longest sequence of consecutive integers. The integers in the sequence do not need to be adjacent in the list, only consecutive in value. The expected complexity is O(n). Return 0 for null or empty input. Duplicates should not extend a sequence.
Examples:
Input: nums = [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive sequence is [1, 2, 3, 4]
Input: nums = [9, 1, 4, 7, 3, 8, 6, 2, 5]
Output: 9
Explanation: All values from 1 to 9 form a consecutive sequence
Input: nums = [1, 2, 0, 1]
Output: 3
Explanation: The duplicate 1 is ignored. The longest sequence is [0, 1, 2]
Input: nums = [5]
Output: 1
Explanation: A single value is its own sequence of length one
Input: nums = null
Output: 0
Explanation: Null input returns zero
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay November 2026 (Nov 16–30)Sponsored by FLXBL
Contest runs November 16 - 30. Complete challenges to climb the leaderboard!
Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.