C++: O(n) solution
\nclass Solution\n{\npublic:\n    int longestConsecutive(vector<int> &nums)\n    {\n        unordered_set<int> num_set;\n        int max_count = 0, count = 0;\n
```\nclass Solution\n{\npublic:\n    int longestConsecutive(vector<int> &nums)\n    {\n        unordered_set<int> num_set;\n        int max_count = 0, count = 0;\n\n        for (auto n : nums) {\n            num_set.insert(n);\n        }\n\n        for (auto n : num_set) {\n            if (!num_set.count(n - 1)) {\n                count = 0;\n                while (num_set.count(n)) {\n                    count++;\n                    n = n + 1;\n                }\n                max_count = max(count, max_count);\n            }\n        }\n        return max_count;\n    }\n};\n```
['C', 'C++']