```
public int longestConsecutive(int[] nums) {
Set<Integer> hash=new HashSet();
for(int i:nums) hash.add(i);
int max=0,count=0;
for(int i:nums)
if(!hash.contains(i-1)){
count=0;
while(hash.contains(i++)){
count++;
}
max=Math.max(max,count);
}
return max;
}
```