Union Find Javascript 648ms
\n/**\n * @param {number[]} A\n * @return {number}\n */\n//UnionFind - 648ms - beats 100%\n\nvar largestComponentSize = function(A) {\n    const p = []\n    for
```\n/**\n * @param {number[]} A\n * @return {number}\n */\n//UnionFind - 648ms - beats 100%\n\nvar largestComponentSize = function(A) {\n    const p = []\n    for(let num of A) {\n        const set = getFactors(num)  // get the set of all the factors\n        for(let factor of set.values()) {  \n            p[find(factor)] = find(num) // union the number with all the factors\n        }\n    }\n    \n    const cntMap = new Map()\n    for(let num of A) {   // for all the input numbers, count the related numbers via their parents \n        const parent = find(num)\n        cntMap.set(parent, (cntMap.get(parent) || 0) + 1) \n    }\n    return Math.max(...cntMap.values())\n    \n    function find(a) {\n        if(!p[a]) p[a] = a\n        while(p[a] !== a) {\n            p[a] = p[p[a]]\n            a = p[a]\n        }\n        return a\n    }\n    \n    function getFactors(number) {\n        const res = new Set()  // use set to guarantee no duplicates, no need to add number itself here. \n        for (let i = 2; i * i <= number; i++) { \n            if (number % i === 0) {\n                res.add(i)\n                res.add(number / i)\n            }\n        }\n        return res\n    }\n};\n```\n
['Union Find', 'JavaScript']