This is 1D version of Number of Islands II. For more explanations, check out this 2D Solution.

`n`

points =`n`

islands =`n`

trees =`n`

roots.- With each edge added, check which island is
`e[0]`

or`e[1]`

belonging to. - If
`e[0]`

and`e[1]`

are in same islands, do nothing. - Otherwise,
**union**two islands, and reduce islands count by`1`

. - Bonus: path compression can reduce time by
`50%`

.

Hope it helps!

```
public int countComponents(int n, int[][] edges) {
int[] roots = new int[n];
for(int i = 0; i < n; i++) roots[i] = i;
for(int[] e : edges) {
int root1 = find(roots, e[0]);
int root2 = find(roots, e[1]);
if(root1 != root2) {
roots[root1] = root2; // union
n--;
}
}
return n;
}
public int find(int[] roots, int id) {
while(roots[id] != id) {
roots[id] = roots[roots[id]]; // optional: path compression
id = roots[id];
}
return id;
}
```