class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> h(nums.begin(), nums.end());
int ans = 0;
for (int num : nums) {
if (!h.count(num - 1)) {
int len = 0;
while (h.count(num++)) ++len;
ans = max(ans, len);
}
}
return ans;
}
};