Using set C++ easy solution


#1
int Solution::longestConsecutive(const vector<int> &A) {
int cnt = 0;
unordered_set<int> mySet;
int n=A.size();
for(int i=0;i<n;i++)
{
   mySet.insert(A[i]); 
}

for(int i=0;i<n;i++)
{
    if(mySet.find(A[i]-1)==mySet.end())
    {
        int curr = 1;
        int num = A[i]+1;
        while(mySet.find(num) != mySet.end())
        {
            curr++;
           num++;
        }
        
        cnt = max(curr,cnt);
    }
}

return cnt;

}