classSolution{public:intlongestConsecutive(vector<int>&nums){intans=0;unordered_set<int>seen{begin(nums),end(nums)};for(intnum:nums){// Num is the start of a sequenceif(seen.count(num-1))continue;intlength=1;while(seen.count(++num))++length;ans=max(ans,length);}returnans;}};
classSolution{publicintlongestConsecutive(int[]nums){intans=0;Set<Integer>seen=Arrays.stream(nums).boxed().collect(Collectors.toSet());for(intnum:nums){// Num is the start of a sequenceif(seen.contains(num-1))continue;intlength=1;while(seen.contains(++num))++length;ans=Math.max(ans,length);}returnans;}}