class Solution {
public int longestConsecutive(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for(int x: nums) set.add(x);
int res = 0;
for(int i: nums)
if(set.contains(i) && !set.contains(i - 1)){
set.remove(i);
int j = i;
while(set.contains(j + 1))
set.remove(++ j);
res = Math.max(res, j - i + 1);
}
return res;
}
}