-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoda.js
More file actions
41 lines (34 loc) · 789 Bytes
/
Copy pathmoda.js
File metadata and controls
41 lines (34 loc) · 789 Bytes
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
36
37
38
39
40
41
/* const lista1 = [
3,
4,
2,
1,
3,
4,
2,
1,
3,
4,
4,
]; */
//* creamos un objeto
function calcularModa(lista1){
const lista1Count = {};
lista1.map(
function(elemento){
if(lista1Count[elemento]){
lista1Count[elemento]++; //*Iteración de elemntos en la lista
}else{
lista1Count[elemento] = 1;
}
}
);
const lista1Array = Object.entries(lista1Count).sort( //*Object.entries devuelve un array con los elementos de un objeto
function(valorAcumulado, nuevoValor){;
return valorAcumulado[1] - nuevoValor[1]; //*Ordenamos los arrays conforme al segundo valor
}
);
const moda = lista1Array[lista1Array.length - 1];
return moda;
}
console.log(calcularModa([]));