```
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size()==0) return 0;
sort(nums.begin(),nums.end());
int length = 1;
int maxLength = 1;
int value = nums[0];
for(int i(1);i<nums.size();i++)
{
if(nums[i] == value+1)
{
length++;
maxLength = max(maxLength,length);
value = nums[i];
}else if(nums[i] != value)
{
length = 1;
value = nums[i];
}
}
return maxLength;
}
};
```