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

105
Vistas
Indexes of duplicate elements in an array - Javascript

I am new to JavaScript and am trying to create a function that would take an array, and return a new array containing true for duplicate elements in the original array, and false otherwise. Here is the code I wrote:

count = function(arr, item) {
  let number = 0
  arr.forEach(elem => {
    if (elem == item) {

      number++

    }
  })
  return number
}
findAllOccurrences = function(arr, target) {
  const duplicatesList = arr.filter(elem => {
    if (count(arr, elem) > 1) {
      return true
    } else {
      return false
    }
  })

  return duplicatesList
}
console.log(findAllOccurrences([1, 2, 2, 3, 4, 2, 5, 4], 2))

But it gives me duplicate values instead of true/false.

Expected output

[false, true, true, false, true, false, false, true]

Actual output

[ 2, 2, 4, 2, 4 ]

What I want to ask is, how does arr.filter function work?

about 4 years ago · Juan Pablo Isaza
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