Skip to content

Commit 28d4e7c

Browse files
authored
Create 1399-count-largest-group.js
1 parent a3b3495 commit 28d4e7c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

1399-count-largest-group.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const countLargestGroup = function(n) {
6+
const hash = {}
7+
const sum = n => `${n}`.split('').reduce((ac, e) => ac + (+e), 0)
8+
for(let i = 1; i <= n; i++) {
9+
const tmp = sum(i)
10+
if(hash[tmp] == null) hash[tmp] = 0
11+
hash[tmp]++
12+
}
13+
// console.log(hash)
14+
const val = Math.max(...Object.values(hash))
15+
let res = 0
16+
Object.keys(hash).forEach(k => {
17+
if(hash[k] === val) res++
18+
})
19+
20+
return res
21+
};

0 commit comments

Comments
 (0)