Java O(n) 90% faster Easy Explanation With comments
\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        int res = 0;// answer len\n        Set<Integer> set = new HashSet<>();\n        for
```\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        int res = 0;// answer len\n        Set<Integer> set = new HashSet<>();\n        for(int i:nums) set.add(i); // add all elements in a set, we dont require duplicates because - \n        //suppose we have 1 1 2 2 3 3 in the array the max len will be 3 - (1 2 3) doesnt matter how many times a number is present \n        for(int i:nums){\n            // the idea is to assume that the present value "i" is the center of the sequence in which it is present \n            // then we will go left and right of it to find the length of its sequence\n            // suppose we have 5 1 2 3 4 6 7 8, now the first element we have is 5\n            int max = 1,prevVal = i-1,nextVal = i+1; // max is the current length as we have one elemet in the sequence that is 5\n            // now for 5 prevVal = 4\n            while(set.contains(prevVal)){ // if 4 is present in the set i.e. in the array\n                max++; // increase the sequence len\n                set.remove(prevVal--); // remove 4 and decremennt prevVal to 3 and continue \n            }\n            // removing the elements because an element can only be part of only one consecutive sequence\n            while(set.contains(nextVal)){ // same for the nextVal of the sequence\n                max++;\n                set.remove(nextVal++);\n            }\n            res = Math.max(res,max); // maintaining max len \n        }\n        return res;\n    }\n}\n```
['Java']