[JS] Union Find in 33 Lines (Beats 100%, Space & Time)
This executes in 444 ms, with no prime-factor optimizations:\n\njs\nvar largestComponentSize = function(nums) {\n  const rootByFactor = new Map();\n  const pare
This executes in 444 ms, with no prime-factor optimizations:\n\n```js\nvar largestComponentSize = function(nums) {\n  const rootByFactor = new Map();\n  const parents = new Array(nums.length);\n  \n  function addFactor(i, factor) {\n    if (rootByFactor.has(factor)) {\n      let r = rootByFactor.get(factor);\n      while (parents[i] != i) i = parents[i];\n      while (parents[r] != r) r = parents[r];\n      parents[i] = r;\n    }\n    rootByFactor.set(factor, parents[i]);\n  }\n  \n  for (const [i, num] of nums.entries()) {\n    parents[i] = i;\n    addFactor(i, num);\n    for (let factor = 2; factor * factor <= num; ++factor) {\n      if (num % factor == 0) {\n        addFactor(i, factor);\n        addFactor(i, num / factor);\n      }\n    }\n  }\n  \n  let largest = 0;\n  const sums = new Array(nums.length).fill(0);\n  for (let r of parents) {\n    while (parents[r] != r) r = parents[r];\n    largest = Math.max(largest, ++sums[r]);\n  }\n  return largest;\n};\n```
['Union Find', 'JavaScript']