1

First of all I apologize this may be trivial but I'm stuck, I search in an array the string that repeats the most and print the 2 that repeat the most. I have wanted to do Case or multiple if, but I also get the idea of a map(), in short I do not find an efficient way to implement it. thank you very much in advance

const clientes=['cliente1','cliente2', 'cliente3', 'cliente2', 'cliente3', 'cliente4'];
for(let x = 0; x < clientes.length; x++) 
{

}
console.log('CLIENTES', clientes)
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79

1 Answers1

0

Loop and count. your approach goes to right direction.

let counts = {};
const clientes = ['cliente1','cliente2', 'cliente3', 'cliente2', 'cliente3', 'cliente4'];

clientes.forEach(function (x) { counts[x] = (counts[x] || 0) + 1; });
console.log(counts)
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79