Count Largest Group LeetCode solution in Java

1
class Solution {
2
public int countLargestGroup(int n) {
3
Map<Integer, Integer> map = new HashMap<>();
4

5
for (int i = 1; i <= n; i++) {
6
int x = sum(i);
7
map.put(x, map.getOrDefault(x, 0) + 1);
8
}
9

10
int max = Collections.max(map.values());
11
int c = 0;
12

13
for (int i : map.values()) {
14
if (i == max) c++;
15
}
16

17
return c;
18
}
19

20
public int sum(int g) {
21
int summ = 0;
22

23
while (g != 0) {
24
int rem = g % 10;
25
summ += rem;
26
g /= 10;
27
}
28

29
return summ;
30
}
31
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0