forked from denulemos/programming-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ejer37.js
30 lines (24 loc) · 850 Bytes
/
ejer37.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
const mostRepeatedElement = (evaluate) => {
let mapping = {}, mostRepeated = "", mostRepeatedTimes = 0; // Guardamos todos los elementos en un JSON
// chequeamos que sea un string
if (typeof evaluate === 'string') {
evaluate = evaluate.split(" ");
}
// mapeamos por palabra y repeticiones
for (let element of evaluate) {
if (mapping[element]) {
mapping[element]++;
} else {
mapping[element] = 1;
}
}
// recorremos el mapeo armado antes
for (let element in mapping) {
if (mapping[element] > mostRepeated) {
mostRepeated = mapping[element];
mostRepeatedTimes = element;
}
}
return `El elemento mas repetido es "${mostRepeatedTimes}" por ${mostRepeated} apariciones`;
}
module.exports = mostRepeatedElement;