Java Solution with UnionFind
\nclass Solution {\n    public int largestComponentSize(int[] A) {\n\n        int maxValue = Arrays.stream(A).reduce(A[0], (x, y) -> x > y ? x : y);\n        \n
```\nclass Solution {\n    public int largestComponentSize(int[] A) {\n\n        int maxValue = Arrays.stream(A).reduce(A[0], (x, y) -> x > y ? x : y);\n        \n        UnionFind uf = new UnionFind(maxValue + 1);\n        \n        // Attribute each element to all the groups that lead by its factors.\n        for (int num : A) {\n            for (int factor = 2; factor < (int)(Math.sqrt(num)) + 1; ++factor)\n                if (num % factor == 0) {\n                    uf.union(num, factor);\n                    uf.union(num, num / factor);\n                }\n        }\n\n        int maxGroupSize = 0;\n        HashMap<Integer, Integer> groupCount = new HashMap<>();\n        for (int num : A) {\n            Integer groupId = uf.find(num);\n            Integer count = groupCount.getOrDefault(groupId, 0);\n            groupCount.put(groupId, count+1);\n            maxGroupSize = Math.max(maxGroupSize, count+1);\n        }\n\n        return maxGroupSize;\n    }\n}\n\nclass UnionFind {\n    int[] parents;\n    int[] ranks;\n\n    public UnionFind(int n) {\n        this.parents = new int[n];\n        this.ranks = new int[n];\n\n        for (int i = 0; i < n; i++) {\n            this.parents[i] = i;\n            this.ranks[i] = 1;\n        }\n    }\n\n    public int find(int x) {\n        if (parents[x] != x) parents[x] = find(parents[x]);\n\n        return parents[x];\n    }\n\n    public void union(int x, int y) {\n        int xRoot = find(x);\n        int yRoot = find(y);\n\n        if (xRoot == yRoot) return;\n\n        if (ranks[xRoot] < ranks[yRoot]) {\n            parents[xRoot] = yRoot;\n        } else {\n            parents[yRoot] = xRoot;\n        }\n\n        if (ranks[xRoot] == ranks[yRoot]) {\n            ranks[xRoot]++;\n        }\n    }\n}\n```
[]