```
public class Solution {
public int longestConsecutive(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int max = 1;
int total = 1;
Arrays.sort(nums);
for(int i = 1; i < nums.length; i++) {
if(nums[i] == nums[i - 1]) continue;
else {
if(nums[i] == nums[i - 1] + 1) {
total += 1;
} else {
total = 1;
}
if(total > max) max = total;
}
}
return max;
}
}
```