Find Duplicates in a JavaScript Array

Cover Image for Find Duplicates in a JavaScript Array

Let's break down how we can find duplicates in a JavaScript array:

  1. Use Set to create a de-duplicated new array
  2. Iterate over the unique array using .reduce
  3. For each value in the unique array, compare the first index to the last index. If the values are unequal, the value occurs multiple times in the original array. If the first and last index values are the same, the value occurs once in the original array.

Find duplicate strings in a JavaScript array

Here's an example that uses the above approach to find duplicate strings within an array:

// Array of strings with duplicate "dog" and "cat" values
const animals = ['dog', 'cat', 'dog', 'cat', 'horse', 'lamb'];

// Use `Set` to de-duplicate the original `animals` array
const uniqueAnimals = [...new Set(animals)];

// Reduce the `uniqueAnimals` into an array of animals that occur more 
// than once in the original `animals` array
const duplicateAnimals = uniqueAnimals.reduce((duplicates, animal) => {
  const firstIndexOfAnimal = animals.indexOf(animal);
  const lastIndexOfAnimal = animals.lastIndexOf(animal);
  
  // Compare the animal's first index to the last index. If the values
  // are not equal, then we can assume the animal string occurs more 
  // than once in the original `animals` array
  if (firstIndexOfAnimal !== lastIndexOfAnimal) {
    return duplicates.concat(animal);
  }

  return duplicates;
}, []);

console.log('duplicateAnimals: ', duplicateAnimals);
// duplicateAnimals:  (2) ['dog', 'cat']

Find duplicate numbers in a JavaScript array

The following approach is similar to the above in that it compares the first and last indexes to determine if the number is a duplicate.

It differs from the first example in that the following example uses .forEach rather than .reduce. This approach may be more comfortable for developers who are unfamiliar with using .reduce.

Some application runtime environments or build processes may not support using Set. Check out 8 Ways to Remove Duplicate Array Values in JavaScript for alternative approaches to de-dupe array values.

const numbers = [0, 1, 2, 3, 1, 4, 5, 3];

// Use `Set` to de-duplicate the original `numbers` array
const uniqueNumbers = [...new Set(numbers)];

const duplicateNumbers = [];

// Use `.forEach` with the new `uniqueNumbers` to compare the first and 
// last indices of each number
uniqueNumbers.forEach((number) => {
  const numbersFirstIndex = numbers.indexOf(number);
  const numbersLastIndex = numbers.lastIndexOf(number);
  const isDuplicateNumber = numbersFirstIndex !== numbersLastIndex;

  // Push the number into the `duplicateNumbers` array if the number 
  // occurs more than one time in the original `numbers` array
  if (isDuplicateNumber) {
    duplicateNumbers.push(number);
  }
})

console.log('duplicateNumbers: ', duplicateNumbers);
// duplicateNumbers:  (2) [1, 3]

Wrapping up

Although we more frequently want to create arrays of unique values, there are situations where it's helpful to know which values occur multiple times within an array.

The above examples provide two of the many ways to find duplicate numbers or strings in a JavaScript array. While these examples may work well for common use cases, you may need to explore alternative approaches if you're working with large data sets.

References

Check out these for additional info on usage: