classSolution { publicintlongestConsecutive(int[] nums) { if (nums == null || nums.length == 0) return0; Set<Integer> set = newHashSet<>(); for (int i : nums) set.add(i); intans=0; for (int num : nums) { intleft= num - 1; intright= num + 1; while (set.remove(left)) left--; while (set.remove(right)) right++; ans = Math.max(ans, right - left - 1); if (set.isEmpty()) return ans;// save time if there are items in nums, but no item in hashset. } return ans; } }