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

🏆 All Time Leaderboard

Contest runs January 1, 1970 - December 31, 9999. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In