```
public int longestConsecutive(int[] nums) {
if(nums.length < 2) return nums.length;
Arrays.sort(nums);
int max = 1;
for (int i = 1, tmp = 1; i < nums.length; i++) {
if (nums[i] == nums[i - 1]) {
continue;
} else if (nums[i] == nums[i - 1] + 1) {
if (++tmp > max) max = tmp;
} else {
tmp = 1;
}
}
return max;
}
```