Union by rank and path compression using C++ || easy to understand
class Solution {\npublic:\n\n    // function to find parent of the node\n     int findPar(int node,vector&parent)\n    {\n        if(parent[node]==node)\n      
class Solution {\npublic:\n\n    // function to find parent of the node\n     int findPar(int node,vector<int>&parent)\n    {\n        if(parent[node]==node)\n        {\n            return node;\n        }\n        \n        return parent[node]=findPar(parent[node],parent);//this line does path compression \n    }\n  //function to connect nodes on the  basis of rank \n \n    void unionn(int u,int v,vector<int>&parent,vector<int>&rank)  \n    {\n        u=findPar(u,parent);\n        v=findPar(v,parent);\n        if(rank[u]>rank[v])\n        {\n            parent[v]=u;\n        }\n        else if(rank[u]<rank[v])\n        {\n            parent[u]=v;\n        }\n        else{\n                 parent[v]=u;\n            rank[u]++;\n        }\n    }\n    int findCircleNum(vector<vector<int>>& isConnected) {\n        \n        int n = isConnected.size();\n        vector<int> parent(n);\n        vector<int>rank(n,0);\n        for(int i=0;i<n;i++)\n        {\n           parent[i]=i;\n        }\n        for(int i=0;i<n;i++)\n        {\n           for(int j=i;j<n;j++)\n           {\n               if(isConnected[i][j])\n               {\n                   unionn(i,j,parent,rank);\n               }\n           }\n        }\n        \n        map<int,int>mp;\n        int count =0;\n        for(int i=0;i<n;i++)\n        {\n           for(int j=i;j<n;j++)\n           {\n               if(isConnected[i][j])\n               {\n                   //if nodes have same parent and not counted before then increase count and remember  the parent\n                   if(findPar(i,parent)==findPar(j,parent) and mp.find(findPar(i,parent))==mp.end())\n                   {\n                       count++;\n                       mp[findPar(i,parent)]=1;\n                   }\n               }\n           }\n        }\n        return count;\n    }\n};
['Union Find', 'C', 'C++']