154. Number of Connected Components

Given the number of vertices n (0..n-1) and an array of undirected edges edges. Return the number of connected components in the graph.
Example 1
Input: n = 5, edges = [[0,1],[1,2],[3,4]]
Output: 2
Explanation: Two components: {0,1,2} and {3,4}.
Example 2
Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]
Output: 1
Explanation: All vertices are connected.
graph
JavaScript
Loading...
Line 1, Char 1