C# | Optimized Union Find | LINQ
\npublic class Solution {\n    public class UnionFind {\n        private int[] root;\n        private int[] rank;\n\n        // O(n)\n        public UnionFind(i
```\npublic class Solution {\n    public class UnionFind {\n        private int[] root;\n        private int[] rank;\n\n        // O(n)\n        public UnionFind(int vertices){\n            root = new int[vertices];\n            rank = new int[vertices];\n            for(int i = 0; i < vertices; i++){\n                root[i] = i;\n                rank[i] = 1;\n            }\n        }\n\n        // O(1), average\n        public int Find(int x){\n            if(x == root[x]){\n                return x;\n            }\n            return root[x] = Find(root[x]);\n        }\n\n        // O(1), average\n        public void Union(int x, int y){\n            int xRoot = Find(x);\n            int yRoot = Find(y);\n            if(xRoot != yRoot){\n                if(rank[xRoot] > rank[yRoot]){\n                    root[yRoot] = xRoot;\n                } else if(rank[xRoot] < rank[yRoot]){\n                    root[xRoot] = yRoot;\n                } else {\n                    root[yRoot] = xRoot;\n                    rank[xRoot]++;\n                }\n            }\n        }\n    }\n\n    public int LargestComponentSize(int[] nums) {\n        int maxValue = nums.Aggregate((x, y) => x > y ? x : y); // Uses LINQ\'s reduce method to find largest value\n        UnionFind ds = new UnionFind(maxValue + 1); // +1 to hold just enough items, without +1 out of bounds\n\n        foreach(int num in nums){\n            for(int factor = 2; factor < (int)(Math.Sqrt(num)) + 1; ++factor){\n                if(num % factor == 0){\n                    ds.Union(num, factor);\n                    ds.Union(num, num / factor);\n                }\n            }\n        }\n\n        int maxGroupSize = 0;\n        Dictionary<int, int> groupCount = new Dictionary<int, int>();\n        foreach(int num in nums){\n            int groupID = ds.Find(num);\n            if(!groupCount.ContainsKey(groupID)){\n                groupCount.Add(groupID, 0);\n            }\n            groupCount[groupID]++;\n            maxGroupSize = Math.Max(maxGroupSize, groupCount[groupID]);\n        }\n\n        return maxGroupSize;\n    }\n}\n```
['Union Find']