Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!