✅ [Python] Union Find ||  Clean and Easy to Understand with explanation
\nclass UnionFindSet(object):\n    def __init__(self):\n        self.data = collections.defaultdict(int)\n\n    def find(self, x):\n        while x <> self.data
```\nclass UnionFindSet(object):\n    def __init__(self):\n        self.data = collections.defaultdict(int)\n\n    def find(self, x):\n        while x <> self.data[x]:\n            x = self.data[x]\n        return x\n\n    def union(self, x, y):\n        if x not in self.data:\n            self.data[x] = x\n        if y not in self.data:\n            self.data[y] = y\n        self.data[self.find(x)] = self.find(y)\n\n    def speedup(self):\n        for x in self.data.keys():\n            self.data[x] = self.find(x)\n\n\nclass Solution(object):\n    def largestComponentSize(self, nums):\n        # First of all, generate all prime numbers less than or equal to the max(nums) sequentially. \n        # It is true that this part of the code is intuitive and easy to understand, but its execution efficiency is not\n        # high. I added a more efficient version in the comment area below, but it might not be that easy to understand.\n        primes = []\n        for num in range(2, max(nums) + 1):\n            flag = True\n            for prime in primes:\n                if num % prime == 0:\n                    flag = False\n                    break\n                if prime * prime > num:\n                    break\n            if flag:\n                primes.append(num)\n\n        # Secondly, find the smallest prime factor of each number.\n        primes = set(primes)\n        smallest_prime_factor = range(max(nums) + 1)\n        for num in range(1, max(nums) + 1):\n            if num in primes:\n                continue\n            for prime in primes:\n                if num % prime == 0:\n                    smallest_prime_factor[num] = prime\n                    break\n\n        # Thirdly, find all prime factors of each number, and generate a dictionary with prime factors as the key and\n        # the list of numbers as the value.\n        prime_factors = collections.defaultdict(list)\n        for num in nums:\n            n = num\n            while smallest_prime_factor[n] < n:\n                prime_factors[smallest_prime_factor[n]].append(num)\n                n //= smallest_prime_factor[n]\n            prime_factors[smallest_prime_factor[n]].append(num)\n\n        # Fourthly, use UnionFindSet to connect numbers with common prime factors.\n        uf = UnionFindSet()\n        for nums in prime_factors.values():\n            for i in range(1, len(nums)):\n                uf.union(nums[i], nums[0])\n        uf.speedup()\n\n        # Finally, returns the number of elements in the largest subset.\n        # Notice that if no edge could union, the UnionFindSet is empty.\n        if len(uf.data) == 0:\n            return 1\n        return collections.Counter(uf.data.values()).most_common(1)[0][1]\n```\n\n**Please UPVOTE!**
['Union Find', 'Python']