Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
674 B
9
Indexable
Never
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> s; // hashmap store key value pair , set just store value
        for(auto i:nums) s.insert(i);
        int longseq=0;
        for(auto i:nums){
            if(s.find(i-1)==s.end()){ // its not a ending of a sequence, start checking for sequence
                int curseq=1;
                while(s.find(i+1)!=s.end()){
                    s.erase(i+1); // HERE
                    curseq++;
                    i++;
                }
                longseq=max(longseq,curseq);
                
            }
        }
        return longseq;
    }
};