Thought I would share it here. May be useful for some one. The algorithm itself is pretty straightforward. But it benefited quite much from the neat expression of C++ idioms. Comments are appreciated!

```
int longestConsecutive(const vector<int> &num) {
unordered_set<int> s(num.begin(), num.end()), searched;
int longest = 0;
for (int i: num) {
if (searched.find(i) != searched.end()) continue;
searched.insert(i);
int j = i - 1, k = i + 1;
while (s.find(j) != s.end()) searched.insert(j--);
while (s.find(k) != s.end()) searched.insert(k++);
longest = max(longest, k - 1 - j);
}
return longest;
}
```