9LOC Java O(n) solution
<pre>\n    public class Solution { <br/>\n        public int longestConsecutive(int[] nums) {<br/>\n          Set<Integer> set = new HashSet<>();\n          int
    <pre>\n    public class Solution { <br/>\n        public int longestConsecutive(int[] nums) {<br/>\n          Set<Integer> set = new HashSet<>();\n          int max = 0;\n          for(int num : nums) set.add(num);\n          for(int num : nums) if (!set.contains(num-1)) {\n            int val = num;\n            while(set.remove(val++));\n            max = Math.max(max, val-num-1);\n          }\n          return max;\n        }\n    }\n    </pre>
[]