Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Union-Find

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Union-Find

Number of Connected Components in an Undirected Graph

intermediatelayer A2 · Union-Find5 tests

You are given n nodes labelled 0 to n - 1 and a list of undirected edges, each [a, b]. Return the number of connected components.

  • Solve it with union-find (disjoint set union)
  • A node with no edges is its own component
  • Duplicate edges are allowed and must not change the answer
  • n = 0 gives 0

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen