Skip to content

Commit 7e01452

Browse files
authored
Create 819-most-common-word.js
1 parent 43171cf commit 7e01452

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

819-most-common-word.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} paragraph
3+
* @param {string[]} banned
4+
* @return {string}
5+
*/
6+
const mostCommonWord = function(paragraph, banned) {
7+
const str = paragraph.toLowerCase()
8+
const arr = str.replace(/\W+/g, ' ').trim().split(' ')
9+
const hash = {}
10+
for(let el of arr) {
11+
if(banned.indexOf(el) !== -1) {
12+
13+
} else {
14+
if(hash.hasOwnProperty(el)) {
15+
hash[el] += 1
16+
} else {
17+
hash[el] = 1
18+
}
19+
}
20+
}
21+
const res = Object.entries(hash).sort((a, b) => b[1] - a[1])
22+
return res[0][0]
23+
};

0 commit comments

Comments
 (0)