Swift Solution (Beats 100%)
This problem was nearly impossible in Swift. I was using Union Find with Path Compression and Weights and it was giving TLE. I finally realized that the Path Co
This problem was nearly impossible in Swift. I was using Union Find with Path Compression and Weights and it was giving TLE. I finally realized that the Path Compression was causing too many array assignments and strangely enough, the solution was faster without path compression. The `group(_ r: Int)` method does not use path compression while the`root(_ r: Int)` method does. I replaced all calls to `root(_ r: Int)` with `group(_ r: Int)` and it provided a 10x improvement.\n\n**Solution**\n```swift\nclass Solution {\n    func largestComponentSize(_ A: [Int]) -> Int {\n        var uf = UnionFind((A.max() ?? 0) + 1)\n        var map = [Int: Int]()\n        \n        for num in A {\n            let primes = factors(num)\n            map[num] = primes[0]\n            \n            for i in 0 ..< primes.count - 1 where primes[i] != primes[i+1] {\n                uf.union(primes[i], primes[i+1])\n            }\n        }\n         \n        var count = [Int: Int]()\n        for num in A {\n            var group = uf.group(map[num]!)\n            count[group, default: 0] += 1\n        }\n        \n        return count.values.max() ?? 0\n    }\n    \n    func factors(_ num: Int) -> [Int] {\n        var result = [Int]() \n        var factor = 2 \n        var num = num\n        \n        var prev = 1\n        while num >= factor * factor {\n            if num % factor == 0 {\n                if factor != prev {\n                    result.append(factor)\n                }\n                prev = factor\n                num = num / factor\n            } else {\n                factor += 1\n            }\n        }\n        \n        result.append(num) \n        return result\n    }\n}\n\n\n\n// MARK: Union Find \n\npublic class UnionFind {\n\t\n\t// MARK: Properties \n\n\tpublic private(set) var arr: [Int]\n\tpublic private(set) var size: [Int]\n\n\n\t// MARK: Initialization \n\t\n\t/// Initializes Union Find with n objects \n\tpublic init(_ n: Int) {\n\t\tself.arr = Array(0 ..< n) \n\t\tself.size = Array(repeating: 1, count: n) \n\t}\n\n\n\t// MARK: Methods \n\n\t/// Connects the set containing p with the set containing q\n\tpublic func union(_ p: Int, _ q: Int) {\n\t\tlet pid = group(p) \n\t\tlet qid = group(q) \n        guard pid != qid else { return }\n\t\tlet (small, big) = size[pid] < size[qid] ? \n\t\t\t(pid, qid) : (qid, pid) \n\t\tsize[big] += size[small]\n\t\tarr[small] = big \n\t}\n\n\t/// Returns true if and only if p and q are connected \n\tpublic func find(_ p: Int, _ q: Int) -> Bool {\n\t\tlet pid = group(p)\n\t\tlet qid = group(q)\n\t\treturn pid == qid \n\t}\n\n\t/// Returns the root of r and performs path compression \n\tpublic func root(_ r: Int) -> Int {\n\t\tvar r = r \n\t\twhile arr[r] != r {\n\t\t\tarr[r] = arr[arr[r]]\n\t\t\tr = arr[r]\n\t\t}\n\t\treturn r \n\t}\n    \n    public func group(_ r: Int) -> Int {\n        var r = r \n        while arr[r] != r {\n            r = arr[r]\n        }\n        return r \n    }\n}\n```
[]