C++ / Python Simple and Short O(n) Solutions
C++:\n\nclass Solution {\npublic:\n    int longestConsecutive(vector<int>& nums) {\n        unordered_map<int, int> m;\n        int res = 0, prev, next;\n      
**C++:**\n```\nclass Solution {\npublic:\n    int longestConsecutive(vector<int>& nums) {\n        unordered_map<int, int> m;\n        int res = 0, prev, next;\n        for (auto num : nums) {\n            if (m.find(num) == m.end()) {\n                prev = m.find(num-1) != m.end() ? m[num-1] : 0;\n                next = m.find(num+1) != m.end() ? m[num+1] : 0;\n                m[num] += prev + next + 1;\n                m[num-prev] = m[num];\n                m[num+next] = m[num];\n                res = max(res, m[num]);\n            }\n        }\n        return res;\n    }\n};\n```\n****\n**Python:**\n```\nclass Solution:\n    def longestConsecutive(self, nums: List[int]) -> int:\n        my_dict = defaultdict(int)\n        maxi = 0\n        for num in nums:\n            if num not in my_dict.keys():\n                prev_ = my_dict.get(num-1, 0)\n                next_ = my_dict.get(num+1, 0)\n                my_dict[num] += prev_ + next_ + 1\n                my_dict[num-prev_] = my_dict[num]\n                my_dict[num+next_] = my_dict[num]\n                maxi = max(maxi, my_dict[num])\n        return maxi\n```
['C', 'Python']