Java Union Find Solution
Save the union size in size[]\n\n\nclass Solution {\n    class DSU {\n        int[] parent;\n        int[] rank;\n        int[] size;\n        DSU (int n) {\n  
Save the union size in `size[]`\n\n```\nclass Solution {\n    class DSU {\n        int[] parent;\n        int[] rank;\n        int[] size;\n        DSU (int n) {\n            parent = new int[n];\n            rank = new int[n];\n            size = new int[n];\n            for (int i = 0; i < n; i++) {\n                parent[i] = i;\n                size[i] = 1;\n            }\n        }\n        int find(int x) {\n            if (parent[x] != x) {\n                parent[x] = find(parent[x]);\n            }\n            return parent[x];\n        }\n        void union(int x, int y) {\n            int rx = find(x);\n            int ry = find(y);\n            if (rx == ry) {\n                return;\n            }\n            if (rank[rx] == rank[ry]) {\n                rank[rx]++;\n                parent[ry] = rx;\n                size[rx] += size[ry];\n            } else if (rank[rx] < rank[ry]) {\n                parent[rx] = ry;\n                size[ry] += size[rx];\n            } else {\n                parent[ry] = rx;\n                size[rx] += size[ry];\n            }\n        }\n    }\n    public int largestIsland(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        DSU dsu = new DSU(m * n);\n        int[][] dirs = new int[][]{{-1, 0}, {0, -1}, {0, 1}, {1, 0}};\n        int result = 1;\n        // Build up the DSU, record the largest union.\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (grid[i][j] == 1) {\n                    for (int d = 0; d < 2; d++) {\n                        int row = i + dirs[d][0];\n                        int col = j + dirs[d][1];\n                        if (row >= 0 && col >= 0 && grid[row][col] == 1) {\n                            dsu.union(i * n + j, row * n + col);\n                            result = Math.max(result, dsu.size[dsu.find(i * n + j)]);\n                        }\n                    }\n                }\n            }\n        }\n        // For each \'0\', calculate the area after changing.\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (grid[i][j] == 0) {\n                    Map<Integer, Integer> map = new HashMap<>();\n                    for (int d = 0; d < 4; d++) {\n                        int row = i + dirs[d][0];\n                        int col = j + dirs[d][1];\n                        if (row >= 0 && col >= 0 && row < m && col < n && grid[row][col] == 1) {\n                            int parent = dsu.find(row * n + col);\n                            map.put(parent, dsu.size[parent]);\n                        }\n                    }\n                    int temp = 1;\n                    for (int v : map.values()) {\n                        temp += v;\n                    }\n                    result = Math.max(result, temp);\n                }\n            }\n        }\n        return result;\n    }\n}\n```
[]