File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Find Most Common Word
3
+ *
4
+ * @param {string } paragraph
5
+ * @param {string[] } banned
6
+ * @return {string }
7
+ */
8
+ var mostCommonWord = function ( paragraph , banned ) {
9
+ const matchedWords = paragraph . match ( / \w + / g) ;
10
+ const hashmap = { } ;
11
+
12
+ let mostCommonWord = '' ;
13
+ let mostCommonWordCount = 0 ;
14
+
15
+ matchedWords . forEach ( wordString => {
16
+ const word = wordString . trim ( ) . toLowerCase ( ) ;
17
+
18
+ if ( ! banned . includes ( word ) ) {
19
+ if ( Object . keys ( hashmap ) . includes ( word ) ) {
20
+ hashmap [ word ] = parseInt ( hashmap ?. [ word ] ?? 0 ) + 1 ;
21
+ } else {
22
+ hashmap [ word ] = 1 ;
23
+ }
24
+
25
+ if ( hashmap [ word ] > mostCommonWordCount ) {
26
+ mostCommonWordCount = hashmap [ word ] ;
27
+ mostCommonWord = word ;
28
+ }
29
+ }
30
+ } ) ;
31
+
32
+ return mostCommonWord ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments