Skip to content

Commit

Permalink
Merge pull request Rohit0301#66 from sarthakw7/sarth
Browse files Browse the repository at this point in the history
Longest consecutive sequence que. solved
  • Loading branch information
Rohit0301 authored Oct 3, 2022
2 parents cf33a2c + 275e5ff commit c249010
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class Solution {
public int longestConsecutive(int[] nums) {
if(nums == null || nums.length == 0) return 0;

Set<Integer> set = new HashSet<>();
for(int n: nums) set.add(n);
int result = 0;
for(int i=0; i<nums.length; i++) {
int res = 1;
if(!set.contains(nums[i] - 1)) {
while(set.contains(++nums[i])) {
res++;
}
}
result = Math.max(res, result);
}
return result;
}
}

0 comments on commit c249010

Please sign in to comment.