JavaScript, how to find duplicates in an array

By

Learn how to find and remove duplicates in a JavaScript array using a Set to dedupe in one line, plus two ways to list which values were actually duplicated.

~~~

If you want to remove the duplicates from a JavaScript array, the quickest way is a Set. A Set is a data structure that only stores unique values, so passing an array through it drops the duplicates. It’s a one-liner:

const scores = [1, 1, 2, 3, 4, 5, 5]
const uniqueScores = [...new Set(scores)]

console.log(uniqueScores) //[ 1, 2, 3, 4, 5 ]

The spread operator turns the Set back into a plain array.

That solves the most common need. But sometimes you don’t want to remove duplicates. You want to know which values were duplicated. Let’s see a few ways to do that.

Find the duplicated values with filter()

My favorite way is to combine filter() with indexOf():

const scores = [1, 1, 2, 3, 4, 5, 5]

const duplicates = scores.filter(
  (item, index) => scores.indexOf(item) !== index
)

console.log(duplicates) //[ 1, 5 ]

indexOf() always returns the position of the first occurrence of a value. So when the current index doesn’t match it, we know we’re looking at a repeat.

One thing to watch: if a value appears three times, it shows up twice in the result. [1, 1, 1, 2] gives you [1, 1]. If you want each duplicated value listed once, wrap the result in a Set:

const uniqueDuplicates = [...new Set(duplicates)]

Find duplicates by sorting

Another solution is to sort a copy of the array, then compare each item with the next one. Equal neighbors mean a duplicate:

const scores = [1, 1, 2, 3, 4, 5, 5]

const duplicates = []
const sorted = [...scores].sort()

for (let i = 0; i < sorted.length; i++) {
  if (sorted[i + 1] === sorted[i]) {
    duplicates.push(sorted[i])
  }
}

console.log(duplicates) //[ 1, 5 ]

Notice I sort a copy made with the spread operator. sort() mutates the array it’s called on, and reordering the original as a side effect of looking for duplicates is a bug waiting to happen.

A caveat about objects

All of this works for primitive values: numbers, strings, booleans. It does not work for objects.

Two objects with the same content are still different objects, so indexOf() and Set both treat them as distinct values. To find duplicate objects you need to decide what makes two of them “equal”, for example comparing an id property, and write the comparison yourself.

~~~

Related posts about js: