Coding Challenge

Number of Connected Components in an Undirected Graph

Medium
graphunion-finddfs

Count connected components in an undirected graph.

Given n nodes and a list of undirected edges, return the number of connected components.

Examples

Input: n = 5, edges = [[0,1],[1,2],[3,4]]

Output: 2

Constraints

  • 0 <= edges.length

Preparing your coding workspace...