Discover more from SWE Prep
Count Largest Group
Lessons Learned: HashMaps
Question: Given an integer
n. Each number from
n is grouped according to the sum of its digits.
Return how many groups have the largest size.
N = 13
Explanation: We arrive at 9 groups in total: [1, 10], [2,11], [3,12], [4,13], , , , , . There are 4 groups with largest size.
Approach 1: HashMap
Our solution will take on the following steps:
Calculate the sum of digits if the number is multidigit
Keep a count of the key and the value (key = sum of digits, value = number)
Keep a track of the max frequency count for the key
Retrieve a list of all the values from the map and count the values which are the same as the max size.