Union Find solution | python
python\nclass Solution:\n    \n    def longestConsecutive(self, nums: List[int]) -> int:\n        \n        if not nums:\n            return 0\n        \n      
```python\nclass Solution:\n    \n    def longestConsecutive(self, nums: List[int]) -> int:\n        \n        if not nums:\n            return 0\n        \n        # remove the duplicate if any\n        nums = set(nums)\n        \n        group, size = {}, {}\n        \n        for n in nums:\n            group[n] = n\n            size[n] = 1\n            \n        def find(a):\n            if group[a] != a:\n                group[a] = find(group[a])\n            return group[a]\n            \n        def union(a, b):\n            ra, rb = find(a), find(b)\n            if ra != rb:\n                group[rb] = ra\n                size[ra] += size[rb]\n        \n        for n in nums:\n            if n - 1 in nums:\n                union(n, n-1)\n            if n + 1 in nums:\n                union(n, n+1)\n        \n        return max(size.values())\n        \n        \n```
[]