Count Largest Group

Lessons Learned: HashMaps

Question: Given an integer n. Each number from 1 to n is grouped according to the sum of its digits. 

Return how many groups have the largest size.

Example:

  • N = 13

  • Output: 4

  • Explanation: We arrive at 9 groups in total: [1, 10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9]. There are 4 groups with largest size.

Solution

Approach 1: HashMap

Our solution will take on the following steps:

  1. Calculate the sum of digits if the number is multidigit

  2. Keep a count of the key and the value (key = sum of digits, value = number)

  3. Keep a track of the max frequency count for the key

  4. Retrieve a list of all the values from the map and count the values which are the same as the max size.

See Approach 1