class{ publicintlongestConsecutive(int[] nums){ if(nums.length == 0){ return0; } HashSet<Integer> numSet = new HashSet(); for(int x : nums){ numSet.add(x); } int res = 1; for(int i = 0; i < nums.length; i++){ if(!numSet.contains(nums[i])){ continue; } numSet.remove(nums[i]); int pre = nums[i] - 1; int next = nums[i] + 1; while(numSet.contains(pre)){ numSet.remove(pre); pre--; } while(numSet.contains(next)){ numSet.remove(next); next++; } res = res > next - pre -1 ? res : next - pre -1; } return res; } }