## 1399. Count Largest Group

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 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], , , , , . There are 4 groups with largest size.
```

Example 2:

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

Example 3:

```Input: n = 15
Output: 6
```

Example 4:

```Input: n = 24
Output: 5
```

Constraints:

• `1 <= n <= 10^4`

## Rust Solution

``````struct Solution;
use std::collections::HashMap;

impl Solution {
fn count_largest_group(n: i32) -> i32 {
let mut hm: HashMap<i32, usize> = HashMap::new();
let mut max = 0;
for i in 1..=n {
let mut sum = 0;
let mut n = i;
while n > 0 {
sum += n % 10;
n /= 10;
}
let count = hm.entry(sum).or_default();
*count += 1;
max = max.max(*count);
}
hm.values().filter(|&&v| v == max).count() as i32
}
}

#[test]
fn test() {
let n = 13;
let res = 4;
assert_eq!(Solution::count_largest_group(n), res);
let n = 2;
let res = 2;
assert_eq!(Solution::count_largest_group(n), res);
let n = 15;
let res = 6;
assert_eq!(Solution::count_largest_group(n), res);
let n = 24;
let res = 5;
assert_eq!(Solution::count_largest_group(n), res);
}
``````

Having problems with this solution? Click here to submit an issue on github.