Using the hashing concept in c++


#1

int Solution::longestConsecutive(const vector &A) {
map<int,int> m;
int result=INT_MIN;
vector help;
int ans=0;
for(int i=0;i<=A.size()-1;i++)
{
m[A[i]]=1;
}

for(auto x:m)
{
    help.push_back(x.first);
}
for(int i=0;i<help.size()-1;i++)
{
    if((help[i+1]-help[i])==1)
    {
        ans++;
    }
    else
    {
        ans=0;
    }
    if(ans>result)
    {
        result=ans;
    }
}
if(help.size()==1)
{
    return 1;
}
return result+1;

}