[10-year recap] Possibly shortest cpp solution, only 6 lines.
Use a hash map to store boundary information of consecutive sequence for each element; there are 4 cases when a new element i reached: \n\n1) neither i+1 nor i-
Use a hash map to store boundary information of consecutive sequence for each element; there are 4 cases when a new element `i` reached: \n\n1) neither `i+1` nor `i-1` has been seen: `m[i]=1`;\n\n2) both `i+1` and `i-1` have been seen: extend `m[i+m[i+1]]` and `m[i-m[i-1]]` to each other;\n\n3) only `i+1` has been seen: extend `m[i+m[i+1]]` and `m[i]` to each other;\n\n4) only `i-1` has been seen: extend `m[i-m[i-1]]` and `m[i]` to each other.\n\n### 2014 version:\n```cpp []\nint longestConsecutive(vector<int> &num) {\n    unordered_map<int, int> m;\n    int r = 0;\n    for (int i : num) {\n        if (m[i]) continue;\n        r = max(r, m[i] = m[i + m[i + 1]] = m[i - m[i - 1]] = m[i + 1] + m[i - 1] + 1);\n    }\n    return r;\n}\n```\n\n### 2024 version for better readability:\n```cpp []\nint longestConsecutive(vector<int>& nums) {\n    unordered_map<int, int> m;\n    int r = 0;\n    for (int i : nums)\n        if (!m[i]) {\n            m[i] = m[i - 1] + m[i + 1] + 1;\n            m[i - m[i - 1]] = m[i];\n            m[i + m[i + 1]] = m[i];\n            r = max(r, m[i]);\n        }\n    return r;\n}\n```
['C++']