# Longest Consecutive Sequence <https://leetcode.com/problems/longest-consecutive-sequence/> ## Sorting Remove redundancy, sort the list, then scan the array. ```python n = len(nums) if n < 2: return n nums = sorted(list(set(nums))) max_len = 1 l_cur = 0 r_cur = 1 while r_cur < len(nums): if nums[r_cur] == nums[r_cur - 1] + 1: pass else: max_len = max(max_len, r_cur - l_cur) l_cur = r_cur r_cur += 1 return max(max_len, r_cur - l_cur) ``` ## DP Utilizing [[dp]], what is the length of a sequence that _ends_ at this number? If there is an adjacent number that is the end of a sequence, extend that sequence. ```python nums = set(nums) table = {} max_len = 0 for num in nums: x = table.get(num - 1, 0) y = table.get(num + 1, 0) length = x + y + 1 table[num - x] = length table[num + y] = length max_len = max(max_len, length) return max_len ```