Java Code
\nclass Solution {\n\tprivate class UnionFind {\n\t\tprivate int[] parent;\n\n\t\tprivate UnionFind(int n){\n\t\t\tparent = new int[n];\n\t\t\tfor(int i=0;i<n;i
```\nclass Solution {\n\tprivate class UnionFind {\n\t\tprivate int[] parent;\n\n\t\tprivate UnionFind(int n){\n\t\t\tparent = new int[n];\n\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\tparent[i] = i;\n\t\t\t}\n\t\t}\n\t\tprivate int getAbsoluteParent(int i){\n\t\t\tif(parent[i]==i){\n\t\t\t\t// absolute parent\n\t\t\t\treturn i;\n\t\t\t}\n\t\t\tparent[i]=getAbsoluteParent(parent[i]);\n\t\t\treturn parent[i];\n\t\t}\n\n\t\tprivate void unify(int i, int j){\n\t\t\tint absoluteParentI = getAbsoluteParent(i);\n\t\t\tint absoluteParentJ = getAbsoluteParent(j);\n\t\t\tif(absoluteParentI!=absoluteParentJ){\n\t\t\t\t// 7->2, 21->2 (7,21)\n\t\t\t\tparent[absoluteParentJ] = absoluteParentI;\n\t\t\t}\n\t\t}\n\n\t}\n\n\tpublic int largestComponentSize(int[] nums) {\n\t\tint max=0;\n\t\tfor(int el:nums) {\n\t\t\tmax = Math.max(el, max);\n\t\t}\n\t\tUnionFind uf = new UnionFind(max+1);\n\n\t\tfor(int i:nums) {\n\t\t\tfor(int j=2;j*j<=i;j++) {\n\t\t\t\tif(i%j==0){ // j is a factor of i\n\t\t\t\t\tuf.unify( j,i); // i=21, j=3\n\t\t\t\t\tuf.unify(i/j,i); // (21,3), (21,21/3(7))\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t//<Parent, Freq>\n\t\tMap<Integer, Integer> map = new HashMap<Integer, Integer>();\n\t\tint maxComponet = 0;\n\t\tfor(int el:nums){\n\t\t\tint parent = uf.getAbsoluteParent(el);\n\t\t\tmap.put(parent, map.getOrDefault(parent, 0) + 1);\n\t\t\tmaxComponet= Math.max(maxComponet, map.get(parent));\n\t\t}\n\n\n\n\t\treturn maxComponet;\n\t}\n}\n```
[]