class Solution {
public:
int longestConsecutive(vector<int> &num) {
unordered_map<int,bool> used;
for(auto n:num)
used[n]=false;
int longest=0;
for(auto n:num)
{
int length=1;
if(used[n]==true)
continue;
for(int j=n+1;used.find(j)!=used.end();j++)
{
length++;
used[j]=true;
}
for(int j=n-1;used.find(j)!=used.end();j--)
{
length++;
used[j]=true;
}
longest=max(longest,length);
}
return longest;
}
};
public:
int longestConsecutive(vector<int> &num) {
unordered_map<int,bool> used;
for(auto n:num)
used[n]=false;
int longest=0;
for(auto n:num)
{
int length=1;
if(used[n]==true)
continue;
for(int j=n+1;used.find(j)!=used.end();j++)
{
length++;
used[j]=true;
}
for(int j=n-1;used.find(j)!=used.end();j--)
{
length++;
used[j]=true;
}
longest=max(longest,length);
}
return longest;
}
};