Python. Union Find
Idea: \n\n1. create set of elements\n2. loop for each x in set. If x + 1 also in set union(x,x+1)\n3. Find parent of maximum size\n\n\npython\nclass Solution:\n
Idea: \n\n1. create set of elements\n2. loop for each x in set. If x + 1 also in set union(x,x+1)\n3. Find parent of maximum size\n\n\n```python\nclass Solution:\n    def longestConsecutive(self, nums: List[int]) -> int:\n        if not nums:\n            return 0\n        \n        UF = {}\n        def find(x):\n            if x != UF[x]:\n                UF[x] = find(UF[x])\n            return UF[x]\n        \n        def union(x,y):\n            UF.setdefault(x,x)\n            UF.setdefault(y,y)\n            UF[find(x)] = find(y)\n        \n        S = set(nums)\n        \n        for n in S:\n            union(n,n)\n            if n+1 in S:\n                union(n+1,n)\n        \n        # find lengths of all the sequences\n        c = Counter()\n        for n in S:\n            c[find(n)]  +=1\n        \n        return max(c.values())\n```\n\nTime: `O(n)`\nSpace: `O(n)`
['Union Find', 'Python']