Easy | C++ | Connected Components
IntuitionApproach

Graph Construction:

Treat each row in properties as a node.
Use intersect() function to determine if two nodes are connected.
If two rows sh
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
1. **Graph Construction:**
   - Treat each row in `properties` as a node.
   - Use `intersect()` function to determine if two nodes are connected.
   - If two rows share at least `k` common elements, create an edge between them in the adjacency list.

2. **Intersection Function (`intersect`):**
   - Convert the first list into an unordered set.
   - Iterate over the second list and count how many elements exist in the set.
   - Remove elements from the set to ensure each element is counted only once.

3. **Finding Connected Components (`countConnectedComponents`):**
   - Use BFS to traverse the graph.
   - Maintain a `visited` array to track visited nodes.
   - For each unvisited node, start a BFS traversal and mark all reachable nodes.
   - Each BFS traversal represents a connected component.
   - Count the number of BFS traversals to get the total number of connected components.

4. **Main Function (`numberOfComponents`):**
   - Calls `numberOfConnectedComponents()` to compute the result.


# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int>g[110];
    vector<bool>vis;
    int findCommon(unordered_map<int,int>&a, unordered_map<int,int>&b){
        auto it = a.begin();
        int ans =0;
        while(it!=a.end()){
            if(b.count(it->first))
                ans++;
            it++;
        }
        return ans;
    }
    void dfs(int u){
        vis[u] = true;
        for(int v:g[u]){
            if(!vis[v]){
                dfs(v);
            }
        }
    }
    int numberOfComponents(vector<vector<int>>& p, int k) {
        vis = vector<bool>(p.size()+10,false);
        vector<unordered_map<int,int>>v;
        for(int i=0;i<p.size();i++){
            unordered_map<int,int>curr;
            for(int j=0;j<p[i].size();j++){
                curr[p[i][j]]++;
            }
            v.push_back(curr);
        }
        for(int i=0;i<p.size();i++){
            for(int j=i+1;j<p.size();j++){
                int ck = findCommon(v[i],v[j]);
                if(ck>=k){
                    g[i].push_back(j);
                    g[j].push_back(i);
                }
            }
        }
        int ans =0;
        for(int i=0;i<p.size();i++){
            if(!vis[i]){
                dfs(i);
                 ans++;
            }
        }
        return ans;
    }
};
```
['Graph', 'C++']