Digit Sum Groups
Easy · hash map
You are given a list of non-negative integers nums. Sort them into groups so that two numbers share a group exactly when the sum of their decimal digits is the same. For example, 13 and 40 both have a digit sum of 4, so they go in the same group, while 22 has a digit sum of 4 as well and joins them. Zero has a digit sum of 0.
Return the size of the largest group. If several groups are tied for the largest size, the size is the same for all of them, so just return it. If the list is empty, return 0.
Examples
Example 1
Input: nums = [13, 31, 22, 40, 5]
Output: 4
13, 31, 22 and 40 all have a digit sum of 4. The number 5 is alone with a digit sum of 5.
Example 2
Input: nums = [12, 21, 5, 30]
Output: 3
12, 21 and 30 have a digit sum of 3. The number 5 forms a group of size 1.
Example 3
Input: nums = []
Output: 0
Constraints
- 0 <= len(nums) <= 10^5
- 0 <= nums[i] <= 10^9