Group Sum by Key
MediumData EngineeringHash TableArrayStringMath
Description
Given an array of [key, value] pairs where key is a string and value is a number, return an object mapping each key to the sum of its values.
Examples
Input:
pairs = [["a",1],["b",2],["a",3]]Output:
{"a":4,"b":2}Explanation:
Summing the values for each key yields {"a":4,"b":2}.
Input:
pairs = [["x",5]]Output:
{"x":5}Explanation:
Summing the values for each key yields {"x":5}.
Input:
pairs = [["k",1],["k",1],["k",1]]Output:
{"k":3}Explanation:
Summing the values for each key yields {"k":3}.
Constraints
- •
1 ≤ pairs.length ≤ 10⁴