[JAVA] Union Find is cool !!
I would highly suggest you to first watch this video before looking at solution.\nhttps://www.youtube.com/watch?v=wU6udHRIkcc\n\nclass Solution {\n    public in
I would highly suggest you to first watch this video before looking at solution.\nhttps://www.youtube.com/watch?v=wU6udHRIkcc\n```\nclass Solution {\n    public int[] findRedundantConnection(int[][] edges) {\n        int n = edges.length;\n        int[] parent = new int[n+1];\n        \n        Arrays.fill(parent,-1);\n        \n        for(int[] e : edges){\n            int p1 = find(e[0],parent);\n            int p2 = find(e[1],parent);\n            if(p1 != p2)\n                union(p1,p2,parent);\n            else\n                return new int[]{e[0],e[1]};\n        }\n        \n        return new int[]{};\n    }\n    \n    private int find(int vertex,int[] parent){\n        while(parent[vertex] > -1)\n            vertex = parent[vertex];\n        \n        return vertex;\n    }\n    \n    private void union(int p1,int p2,int[] parent){\n        int totalNodes = parent[p2] + parent[p1];\n        if(parent[p1] <= parent[p2]){\n            parent[p2] = p1;\n            parent[p1] = totalNodes;\n        }else{\n            parent[p1] = p2;\n            parent[p2] = totalNodes;\n        }\n    }\n}\n```
['Union Find', 'Java']