The idea here is to create a map of element [ i ] ==> length of consecutive sequence upto element i .

The answer would simply be the maximum value of second element of the map.

Again we all are assuming map lookups are in O(1) .

```
class Solution {
public:
int longestConsecutive(vector<int> &num) {
map<int,int> mp;
for(int i=0;i<num.size();++i)
mp[num[i]] = 1;
for(map<int,int>::iterator i=mp.begin();i!=mp.end();++i){
if(mp.find(i->first-1) != mp.end()){
i->second = mp[i->first-1]+1;
}
}
int ans=0;
for(map<int,int>::iterator i=mp.begin();i!=mp.end();++i){
ans = max(ans,i->second);
}
return ans;
}
};
```