C++ Solution using unordered_set


#1
#include<bits/stdc++.h>

int Solution::longestConsecutive(const vector<int> &A) {
    if(A.size()==1) return 1;
    unordered_set<int> st;
    int maxlen=0;
    
    for(int i=0; i<A.size(); i++)
        st.insert(A[i]);
    
    for(int i=0; i<A.size(); i++){
        if(st.find(A[i]-1) != st.end())
            continue;
        
        int n = A[i], currlen = 1;
        while(st.find(n+1) != st.end()){
            currlen++; n++;
        }
        maxlen = max(maxlen, currlen);
    }
    return maxlen;
}