You are given an integer n.

Each number from 1 to n is grouped according to the sum of its digits.

Return the number of groups that have the largest size.

&nbsp;
Example 1:

Input: n = 13
Output: 4
Explanation: There are 9 groups in total, they are grouped according sum of its digits of numbers from 1 to 13:
[1,10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9].
There are 4 groups with largest size.


Example 2:

Input: n = 2
Output: 2
Explanation: There are 2 groups [1], [2] of size 1.


&nbsp;
Constraints:


	1 &lt;= n &lt;= 104


def compute(num):
	if num < 10:
		return num

	newVal = 0

	while num > 0:
		last = num % 10
		newVal += last
		num /= 10

	return newVal

class Solution(object):
	def countLargestGroup(self, n):
		"""
		:type n: int
		:rtype: int
		"""
		myMap = {}

		for i in range(1, n + 1):
			val = compute(i)

			if val in myMap.keys():
				myMap.get(val).append(i)
			else:
				myMap[val] = [i]

		maxLen = 0

		for n in myMap.values():
			maxLen = max(maxLen, len(n))

		ans = 0

		for n in myMap.values():
			if len(n) == maxLen:
				ans += 1

		return ans
class Solution {
    public int countLargestGroup(int n) {
        Map<Integer,Integer> map=new HashMap<>();
        for(int i=1;i<=n;i++){
            int x=sum(i);
            map.put(x,map.getOrDefault(x,0)+1);
        }
        int max=Collections.max(map.values());
        int c=0;
        for(int i:map.values()){
            if(i==max) c++;
        }
        return c;
    }
    public int sum(int g){
        int summ=0;
        while(g!=0){
            int rem=g%10;
            summ+=rem;
            g/=10;
        }
        return summ;
    }
}```
class Solution {
public:
    int cal(int n){
        int sum = 0;
        while(n > 0){
            sum += n%10;
            n /= 10;
        }
        return sum;
    }

    int countLargestGroup(int n) {
        int a[40];
        for(int i=0; i<40; i++) a[i] = 0;

        for(int i=1; i<=n; i++){
            a[cal(i)]++;
        }

        int max = 0;
        int count = 0;
        for(int i=0; i<40; i++){
            if(a[i] > max){
                count = 1;
                max = a[i];
            }
            else if(a[i] == max) count++;
        }

        return count;
    }
};
var countLargestGroup = function(n) {
    const hash = {};

    for (let i = 1; i <= n; i++) {
        const sum = i.toString().split('').reduce((r, x) => r + parseInt(x), 0);

        if (!hash[sum]) {
            hash[sum] = 0;
        }

        hash[sum]++;
    }

    return Object.keys(hash)
        .sort((a, b) => hash[b] - hash[a])
        .reduce((res, x) => {
            const prev = res[res.length - 1];

            if (!prev || prev === hash[x]) {
                res.push(hash[x]);
            }

            return res;
        }, []).length;
};
Count Largest Group