Another accepted Java O(n) solution
public int longestConsecutive(int[] num) {\n      int max = 0;\n      \n      Set<Integer> set = new HashSet<Integer>();\n      for (int i = 0; i < nums.length;
    public int longestConsecutive(int[] num) {\n      int max = 0;\n      \n      Set<Integer> set = new HashSet<Integer>();\n      for (int i = 0; i < nums.length; i++) {\n        set.add(nums[i]);\n      }\n      \n      for (int i = 0; i < nums.length; i++) {\n        int count = 1;\n        \n        // look left\n        int num = nums[i];\n        while (set.contains(--num)) {\n          count++;\n          set.remove(num);\n        }\n        \n        // look right\n        num = nums[i];\n        while (set.contains(++num)) {\n          count++;\n          set.remove(num);\n        }\n        \n        max = Math.max(max, count);\n      }\n      \n      return max;\n    }
['Java']