973. Count Degree of Each Vertex

EasyGraphsRepresentationGraph

Given an undirected graph with n nodes and a list of edges, return the degree of each node (the number of edges incident to it) as an array indexed by node. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Array — the degree of each node.

Examples

Example 1
Input: {"n":5,"edges":[[0,1],[0,2],[1,3],[2,4]]}
Output: [2,2,2,1,1]
Explanation: Incident edge counts.
Example 2
Input: {"n":1,"edges":[]}
Output: [0]
Explanation: Isolated node.
Example 3
Input: {"n":3,"edges":[[0,1],[1,2],[2,0]]}
Output: [2,2,2]
Explanation: Triangle: each degree 2.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →