O(n) HashMap Java Solution
Use a hashmap to map a number to its longest consecutive sequence length, each time find a new consecutive sequence, only the begin number and end number need t
Use a hashmap to map a number to its longest consecutive sequence length, each time find a new consecutive sequence, only the begin number and end number need to be modified.\n\n    public class Solution {\n        public int longestConsecutive(int[] num) {\n            int longest = 0;\n            Map<Integer, Integer> map = new HashMap<Integer, Integer>();\n            for(int i = 0;i < num.length;i++){\n                // if there is no duplicates, these two lines can be commented\n                if(map.containsKey(num[i])) continue;\n                map.put(num[i],1);\n                \n                int end = num[i];\n                int begin = num[i];\n                if(map.containsKey(num[i]+1))\n                    end = num[i] + map.get(num[i]+1);\n                if(map.containsKey(num[i]-1))\n                    begin = num[i] - map.get(num[i]-1);\n                longest = Math.max(longest, end-begin+1);\n                map.put(end, end-begin+1);\n                map.put(begin, end-begin+1);\n            }\n            return longest;\n        }\n    }
['Java']