classSolution { publicintlongestConsecutive(int[] nums) { Map<Integer, Boolean> map = newHashMap<>(nums.length); for (int num : nums) { map.put(num, false); }
intmaxCount=0; for (int num : nums) { intcount=0; if (!map.get(num)) { count++; intnext= num + 1; while (!map.getOrDefault(next, true)) { map.put(next, true); count++; next = next + 1; }
intfront= num - 1; while (!map.getOrDefault(front, true)) { map.put(front, true); count++; front = front - 1; }