-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path135-Maps.js
35 lines (27 loc) · 812 Bytes
/
135-Maps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
Dado un array de palabras, devolver la palabra mas repetida de la misma
* Input: ["hola", "como", "estas", "hola", "hola"]
* Output: "hola"
*/
const palabraMasRepetida = (array) => {
const stringCount = new Map();
array.forEach(element => {
element = element.toLowerCase();
if (stringCount.has(element)) {
stringCount.set(element, stringCount.get(element) + 1);
}
else {
stringCount.set(element, 1);
}
});
let maxCount = 0;
let maxKey = "";
stringCount.forEach((value, key) => {
if (value > maxCount) {
maxCount = value;
maxKey = key;
}
});
return `${maxKey} ${maxCount}`;
}
console.log(palabraMasRepetida(["hola","hola","hola", "hola", "denu", "chau"])); // hola 4