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

259
Views
Get the index of duplicate values except the first

Below is an example array:

const attD = [1, 2, 3, 4, 3, 3]

How can we get the index of the duplicate values from this array? In this example, 3 is repeated thrice. So, I want to get the index 4, index 5 as these are duplicated at that positions (ignoring the first occurrence).

I have tried the below code, but it is giving me values. Instead, I need the index.

const findDuplicates = attD => attD.filter((item, index) => attD.indexOf(item) !== index);
const duplicates = findDuplicates(attD);
console.log(duplicates);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. With .map(), add the index of duplicated values into the array. For non-duplicate value, will be added with undefined value [Will be filtered in Step 2].
  2. With .filter(), remove the value which is undefined from the array.

const attD = [1, 2, 3, 4, 3, 3];

const findDuplicates = attD => attD.map((item, index) => {
  if (attD.indexOf(item) !== index)
    return index;
}).filter(x => x);  // Equivalent to .filter(x => x != undefined || x != null)

const duplicates = findDuplicates(attD);
console.log(duplicates);

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