Easy solution using Map and Set
Approach

Use a map array to store the connected node.


Check every possible pair. For each pair i,j:

If 2 nodes i&j are connected, store the smallest of ther
# Approach
<!-- Describe your approach to solving the problem. -->
- Use a map array to store the connected node.
- Check every possible pair. For each pair i,j:
     - If 2 nodes i&j are connected, store the smallest of there map values in both map[i] and map[j].

- Add the minimum value of the connected nodes in set
- return the size of set

# Code
```java []
class Solution {
    public int numberOfComponents(int[][] p, int k) {
        Set<Integer> set=new HashSet<>();
        int map[]=new int[p.length];
        for(int i=0;i<p.length;i++)
            map[i]=i;

        for(int i=0;i<p.length;i++){
            for(int j=0;j<p.length;j++){
                if(i==j)
                    continue;
                if(check(p[i],p[j],k)){
                    int x=Math.min(map[i],map[j]);
                    map[i]=x;
                    map[j]=x;
                }
            }
        }
                
        for(int i=0;i<p.length;i++){
            int x=i;
            while(map[x]!=x)
                x=map[x];
            set.add(x);
        }

        return set.size();
    }
    boolean check(int[] a, int[] b, int k){
        Set<Integer> set=new HashSet<>();
        for(int it:a)
            set.add(it);
        for(int it:b)
            if(set.contains(it)){
                k--;
                set.remove(it);
            }
        return k<=0;
    }
    
}
```
['Java']