Java 7ms O(N) solution
\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        Map<Integer, Integer> counts = new HashMap<>();\n        int max = 0;\n        for 
```\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        Map<Integer, Integer> counts = new HashMap<>();\n        int max = 0;\n        for (int i = 0; i < nums.length; i++) {\n            if (counts.containsKey(nums[i])) continue;\n            // |---count down ---| nums[i] |--- count up ---|\n            int countDown = counts.getOrDefault(nums[i] - 1, 0);\n            int countUp = counts.getOrDefault(nums[i] + 1, 0);\n            int count = countUp + countDown + 1;\n\t\t\t// Now update count for the current number.\n            counts.put(nums[i], count);\n\t\t\t// And on each edge. No need to update in between as we skip those elements anyways and they don\'t matter.\n            counts.put(nums[i] + countUp, count);\n            counts.put(nums[i] - countDown, count);\n            if (count > max) {\n                max = count;\n            }\n        }\n        return max;\n    }\n}\n```
[]