class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) set.add(num);
int max = 0;
for (int num : set) {
int length = 1;
if (!set.contains(num-1)) {
int target = num+1;
while (set.contains(target++))
length++;
}
max = Math.max(max, length);
}
return max;
}
}