Python O(n) solution using sets
class Solution:\n        # @param num, a list of integer\n        # @return an integer\n        def longestConsecutive(self, num):\n            num=set(num)\n  
    class Solution:\n        # @param num, a list of integer\n        # @return an integer\n        def longestConsecutive(self, num):\n            num=set(num)\n            maxLen=0\n            while num:\n                n=num.pop()\n                i=n+1\n                l1=0\n                l2=0\n                while i in num:\n                    num.remove(i)\n                    i+=1\n                    l1+=1\n                i=n-1\n                while i in num:\n                    num.remove(i)\n                    i-=1\n                    l2+=1\n                maxLen=max(maxLen,l1+l2+1)\n            return maxLen
['Python']