Simple fast Java solution using Set
Using a set to collect all elements that hasn't been visited. search element will be O(1) and eliminates visiting element again.\n\n    public class Solution {\
Using a set to collect all elements that hasn't been visited. search element will be O(1) and eliminates visiting element again.\n\n    public class Solution {\n    public int longestConsecutive(int[] nums) {\n        if(nums == null || nums.length == 0) return 0;\n        \n        Set<Integer> set = new HashSet<Integer>();\n        \n        for(int num: nums) set.add(num);\n        int max = 1;\n        for(int num: nums) {\n            if(set.remove(num)) {//num hasn't been visited\n                int val = num;\n                int sum = 1;\n                while(set.remove(val-1)) val--;\n                sum += num - val;\n                \n                val = num;\n                while(set.remove(val+1)) val++;\n                sum += val - num;\n                \n                max = Math.max(max, sum);\n            }\n        }\n        return max;\n    }\n    }
['Java']