```
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> st(nums.begin(),nums.end());
int ret = 0;
while(st.size())
{
int x = *st.begin();
int left = x,right = x+1;
while(st.count(left))
st.erase(left--);
while(st.count(right))
st.erase(right++);
ret = max(ret,right - left - 1);
}
return ret;
}
};
```