Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
How can I find anagrams in an array of words?

I am trying to get all anagrams from a array of words:

arr = ['cab','bac','tru']

The expected output is supposed to be:

{abc: ['cab','bac']}

I tried to implement the below code:

var words = ['cab', 'bac', 'mihir']
let result = {}

words.forEach(word => {
  var a = word.split("").sort().join("");
  result[word] = a

})

console.log(result)

How do I iterate over the values so that I can access the keys if they have the same values?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use the sorted words as keys in an object, and collect each word that matches the key in an array:

var words = ['cab', 'bac', 'mihir']
let result = {}

for (const word of words) {
  const sorted = word.split("").sort().join("");

  if (sorted in result) {
    // If there is already an entry in the result, append this word
    result[sorted].push(word);
  } else {
    // Otherwise, create one
    result[sorted] = [word];
  }
}

console.log(result);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda