Skip to content

Commit

Permalink
Merge pull request neetcode-gh#859 from MaratKhakim/684-Redundant-Con…
Browse files Browse the repository at this point in the history
…nection-GO

GO: 684. Redundant Connection
  • Loading branch information
Ahmad-A0 authored Aug 20, 2022
2 parents 501c7dc + 0e46da9 commit a616539
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions go/684-Redundant-Connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
func findRedundantConnection(edges [][]int) []int {
parent := make([]int, len(edges)+1)

for i := 0; i < len(parent); i++ {
parent[i] = i
}

for _, edge := range edges {
if find(parent, edge[0]) == find(parent, edge[1]) {
return edge
}

unify(parent, edge[0], edge[1])
}

return []int{}
}

func find(parent []int, num int) int {
if parent[num] == num {
return num
}

return find(parent, parent[num])
}

func unify(parent []int, x, y int) {
parent[find(parent, y)] = find(parent, x)
}

0 comments on commit a616539

Please sign in to comment.