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

241
Vistas
How to find the common elements in 2 arrays in Javascript with no duplicates (unless there are 2 of the same element)?

For example, if I have 2 arrays being

['dog', 'cat', 'zebra', 'lion', 'cat']

and

['dog', 'cat', 'cat', 'frog']

then a new array should be created that only contains

['dog', 'cat', 'cat']
almost 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use two loop for the same and compare them from each other. Ex.

for(let i=0; i<ar1.length; i++)
 {
     for(let j=0; j<ar2.length; j++){
       if(ar1[i]==ar[2])
           res.push(ar[i]);
almost 4 years ago · Santiago Trujillo Denunciar

0

var array1 = ['dog', 'cat', 'zebra', 'lion', 'cat'];
var array2 = ['dog', 'cat', 'cat', 'frog'];

// Then you can use filter

// const filteredArray = array1.filter(value => array2.includes(value));


// (OR) `indexOf` for old browsers compability

var filteredArray = array1.filter(function(n) {
  return array2.indexOf(n) !== -1;
});

console.log(filteredArray);

almost 4 years ago · Santiago Trujillo Denunciar

0

const a = ['dog', 'cat', 'zebra', 'lion', 'cat'];
const b = ['dog', 'cat', 'cat', 'frog'];

let result = a.filter(function(obj){
    return b.indexOf(obj) !== -1
})
  
console.log(result)

almost 4 years ago · Santiago Trujillo 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