Implement the cloneGraphNodeCount method that returns how many nodes are present after cloning a graph.

The input contains an edge list edges, where each pair represents a connection between two graph nodes.

Your task is to return how many unique nodes would be present after cloning the graph. Since the output is only a count, you do not need to return the cloned graph structure.

For example, edges [[1,2],[2,3]] contain the unique nodes 1, 2, and 3, so the answer is 3.

Example 1

Input
edges (int[][]) = [[1,2],[2,3]]
size (int) = 2
Returns (int) 3

Example 2

Input
edges (int[][]) = []
size (int) = 0
Returns (int) 0

Example 3

Input
edges (int[][]) = [[1,2],[2,3],[3,1]]
size (int) = 3
Returns (int) 3

A graph clone creates one new node for every original node. Therefore, the number of cloned nodes is equal to the number of unique node values found in the graph.

Scan all edges and add both endpoints to a set. A set automatically stores each node only once.

After all edges are processed, return the size of the set.

Pseudocode:

function cloneGraphNodeCount(edges, size):
    uniqueNodes = empty set
    for each edge in edges:
        add edge[0] to uniqueNodes
        add edge[1] to uniqueNodes
    return size of uniqueNodes
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.