hardArrays & HashingPure DSA~30 min
Longest Consecutive Session Streak
Each active day for a user is recorded as an integer day-index, unsorted and possibly duplicated. Find the length of the longest run of consecutive active days to award a streak badge.
Problem
Given an unsorted array of integers nums, return the length of the longest sequence of consecutive integers. The algorithm must run in O(n) time.
Input
An array nums of n integers (0 ≤ n ≤ 10^5, −10^9 ≤ nums[i] ≤ 10^9).
Output
An integer: the length of the longest consecutive run.
Constraints
- 0 ≤ n ≤ 10^5
- Values may be negative and may repeat
- Required time complexity is O(n)
Examples
Example 1
Input
nums = [100,4,200,1,3,2]
Output
4
The run 1,2,3,4 has length 4.
Example 2
Input
nums = [0,3,7,2,5,8,4,6,0,1]
Output
9
0..8 forms a run of length 9 (the duplicate 0 is ignored).