Skip to content

Instantly share code, notes, and snippets.

@akshay-ravindran-96
Created April 11, 2022 13:34
Show Gist options
  • Save akshay-ravindran-96/6b70c9ee086f62d8759dd4195cb42c8a to your computer and use it in GitHub Desktop.
Save akshay-ravindran-96/6b70c9ee086f62d8759dd4195cb42c8a to your computer and use it in GitHub Desktop.
longestConsecutive_graph.java
class Solution {
public int longestConsecutive(int[] nums) {
if (nums.length == 0) return 0;
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
}
int res = 1;
for (int i = 0; i < nums.length; i++) {
if(set.contains(nums[i]-1)) continue;
int counter=0;
while (set.contains(nums[i] + counter)){
set.remove(nums[i]+counter);
counter++;
}
res = Math.max(res,counter);
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment