Skip to content

Instantly share code, notes, and snippets.

@ksamirdev
Created April 6, 2026 15:18
Show Gist options
  • Select an option

  • Save ksamirdev/2b079ac0df811448a0db7ddea4873e1b to your computer and use it in GitHub Desktop.

Select an option

Save ksamirdev/2b079ac0df811448a0db7ddea4873e1b to your computer and use it in GitHub Desktop.
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
s = set(nums)
longest = 0
for num in s:
if num - 1 not in s:
next_num = num + 1
length = 1
while next_num in s:
length += 1
next_num += 1
longest = max(longest, length)
return longest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment