class Solution {

public:

int longestConsecutive(vector<int> &num) {

```
int sz=num.size();
if(sz<=1)
return sz;
int ret=0;
unordered_set<int> s(num.begin(),num.end());
for(unordered_set<int>::iterator it=s.begin(); it!=s.end(); s.erase(it++) )
{
int tmp=1, n=*it;
while(s.count(n-1))
{
++tmp;
s.erase(--n);
}
n=*it;
while(s.count(n+1))
{
++tmp;
s.erase(++n);
}
ret=max(ret,tmp);
}
return ret;
}
```

};