C++ Soln. Union By Rank and Path Compression
If You Like it Please Upvote. If you have any doubts feel free to ask in the comments.\n\nclass Solution {\npublic:\n    int find(int parent[],int s){\n        
**If You Like it Please Upvote. If you have any doubts feel free to ask in the comments.**\n```\nclass Solution {\npublic:\n    int find(int parent[],int s){\n        if(parent[s]!=s){\n            parent[s]=find(parent,parent[s]);\n        }\n        return parent[s];\n    }\n    void Union(int parent[],int rank[],int x,int y){\n        if(rank[x]>rank[y]){\n            parent[y]=x;\n        }\n        else if(rank[y]>rank[x]){\n            parent[x]=y;\n        }\n        else{\n            parent[x]=y;\n            rank[y]++;\n        }   \n    }\n    vector<int> findRedundantConnection(vector<vector<int>>& edges) {\n        vector<int>ans;\n        int n=edges.size();\n        int parent[n+1];\n        int rank[n+1];\n        for(int i=1;i<=n;++i){\n            parent[i]=i;\n            rank[i]=0;\n        }\n        for(int i=0;i<edges.size();++i){\n            int x=find(parent,edges[i][0]);\n            int y=find(parent,edges[i][1]);\n            if(x==y){\n                ans.push_back(edges[i][0]);\n                ans.push_back(edges[i][1]);\n                break;\n            }\n            else{\n                Union(parent,rank,x,y);\n            }\n        }\n        return ans;\n    }\n};\n```
[]