```
public int longestConsecutive(int[] nums) {
if (nums.length == 0) return 0;
Arrays.sort(nums);
int max = 0, start = 0, end = 0;
while (++end < nums.length) {
if (nums[end] == nums[end-1]){
start++;
}else if (nums[end] != nums[end-1]+1){
max = Math.max(max, end-start);
start = end;
}
}
return Math.max(max, end-start);
}
```