FunctionRacerProblemsRace

Count the Links

BeginnerGraphsPython, JavaScript, TypeScript, Java, C++

A network is given as neighbor lists: neighbors[i] lists the nodes that node i is linked to. Links work in both directions, so whenever j is in neighbors[i], i is in neighbors[j] too.

Return the number of links in the network.

Example 1

neighbors [[1,2],[0],[0]]
returns 2

The links are 0 with 1 and 0 with 2. Each of them shows up in two lists.

Example 2

neighbors [[1,2],[0,2],[0,1]]
returns 3

A triangle has three links.

Example 3

neighbors [[]]
returns 0

A single node has no links.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems