DFS approach with Cycle detection - intuitive approach
Basically am adding edges one by one in graph and as soon as adding any edge creates the cycle, I will return the result.\n\n\n    public int[] findRedundantCon
Basically am adding edges one by one in graph and as soon as adding any edge creates the cycle, I will return the result.\n\n\n    public int[] findRedundantConnection(int[][] edges) {\n        Map<Integer, Set<Integer>> gp = new HashMap<>();  \n\t\tSet<Integer> visited = new HashSet<>();\n        for (int[] edge: edges) {\n            gp.putIfAbsent(edge[0], new HashSet<>());\n            gp.putIfAbsent(edge[1], new HashSet<>());\n            gp.get(edge[0]).add(edge[1]);\n            gp.get(edge[1]).add(edge[0]);\n           \n            if (isCycle(gp, edge[0], visited, -1)) {\n                return new int[] {edge[0], edge[1]};\n            }\n\t\t\tvisited.clear();\n        }\n        \n        return new int[] {}; //shouldnt happen\n    }\n    \n    private boolean isCycle(Map<Integer, Set<Integer>> gp, int node, Set<Integer> visited, int pre) {\n        if (visited.contains(node)) {\n            return true;\n        }\n        visited.add(node);\n        if (gp.get(node) != null) {\n            for (int child: gp.get(node)) {\n                if (child == pre) {\n                    continue;\n                }\n                if (isCycle(gp, child, visited, node)) {\n                    return true;\n                } \n            } \n        }\n        return false;\n    }
[]