Redundant Connection
A tree is an undirected graph that is connected and has no cycles. You start with a tree on n nodes labelled 1 to n. Then one extra edge is added between two different nodes that were not already directly joined.
The resulting graph is given as edges, an array of n edges where edges[i] = [a, b] is an undirected edge between a and b.
Return an edge that can be removed so that the remaining n - 1 edges form a tree on all n nodes. If more than one edge would work, return the one that appears last in edges. Return it exactly as it is written in the input.
Constraints
3 <= n <= 1000, andedges.length === n1 <= a, b <= nanda !== b- No edge appears twice, and the graph is connected.
Examples
Example 1
Input: edges = [[1,2],[1,3],[2,3]] Output: [2,3]
The three edges form the triangle 1-2-3. Removing any of them leaves a tree, and [2, 3] is the one listed last.
Example 2
Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]] Output: [1,4]
1-2-3-4-1 is a cycle. Of its four edges, [1, 4] appears last in the input. [1, 5] is not on the cycle, so removing it would disconnect node 5.
Example 3
Input: edges = [[1,2],[2,3],[1,3],[3,4],[4,5]] Output: [1,3]
The cycle 1-2-3 closes at the third edge. The answer does not have to be the last edge of the whole list.
JavaScript you'll use: Classes for data structures Destructuring, spread and rest
/**
* @param {number[][]} edges
* @return {number[]}
*/
function findRedundantConnection(edges) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.