```
class Solution {
public:
int longestConsecutive(std::vector<int> &num) {
if (num.size() < 2)
return num.size();
std::set<int> nums;
for (int i = 0; i != num.size(); ++i)
nums.insert(num[i]);
std::set<int>::const_iterator it = nums.begin();
int length = 1, start = *it, max_length = length;
for (++it; it != nums.end(); ++it)
if (*it != start + length) {
max_length = std::max(max_length, length);
length = 1;
start = *it;
}
else
++length;
return std::max(max_length, length);
}
};
```