class Solution {
public int longestConsecutive(int[] nums) {
int n = nums.length;
if (n == 0) return 0;
int longestLength = 1;
Set<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++) {
set.add(nums[i]);
}
for (int i : set) {
if (!set.contains(i - 1)) {
int count = 1;
int x = i;
while (set.contains(x + 1)) {
x++;
count++;
}
longestLength = Math.max(count, longestLength);
}
}
return longestLength;
}
}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)