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