Java Straightforward Solution
Use a HashMap. Use every number in num[] as a key, mark them as not used.\n> \n> 2. For each number in the hashmap, if the number is not used, select it as the 
> 1. Use a HashMap<Integer,Boolean>. Use every number in num[] as a key, mark them as not used.\n> \n> 2. For each number in the hashmap, if the number is not used, select it as the center to grow a sequence on number++ and number--\n> directions. Mark used number as used.\n> \n> 3. After each growth, update the longestSoFar. \n> \n> 4. Return the longestSoFar.  \n> \n> 5. Since each number is used only once, the time complexity is O(n).\n\n    public class Solution {\n        public int longestConsecutive(int[] num) {\n            if(num.length<=1)\n                return num.length;\n            HashMap<Integer,Boolean> numberUsed=new HashMap<Integer,Boolean>();\n            for(int i:num)\n                numberUsed.put(i,false);\n            int longestSoFar=1;\n            for(int i:num)\n            {\n                if(numberUsed.get(i)==false)\n                {\n                    int localLongest=1;\n                    int tmp=i+1;\n                    numberUsed.put(i,true);\n                    while(numberUsed.containsKey(tmp)&&numberUsed.get(tmp)==false)\n                    {\n                        localLongest+=1;\n                        numberUsed.put(tmp,true);\n                        tmp+=1;\n                    }\n                    tmp=i-1;\n                    while(numberUsed.containsKey(tmp)&&numberUsed.get(tmp)==false)\n                    {\n                        localLongest+=1;\n                        numberUsed.put(tmp,true);\n                        tmp-=1;\n                    }\n                    longestSoFar=Math.max(longestSoFar,localLongest);\n                }\n            }\n            return longestSoFar;\n        }\n    }
['Java']