How To Count Duplicate Values in An Array in JavaScript – Definitive Guide

JavaScript arrays allow you to store duplicate values.

You can count duplicate values in an array in JavaScript using the foreach() and elementCounts[element] = (elementCounts[element] || 0) + 1 statement.

This tutorial teaches you how to count duplicate values in an array in JavaScript and when it is appropriate to use each method.

Using ForEach

ForEach() iterates over each element in the array and allows you to perform an action during each iteration.

To count the duplicate values,

  • iterate over the array using foreach()
  • Add the count of each element in an associative set and add the count in that index
  • If the element index already exists, increment the count during each iteration. Else, initialise the count to 1

This is a simple method to count the duplicates in an array. Use this method when you want the count of each element to be stored in a set.

Code

The following code demonstrates how to use foreach() and associative set to count the duplicates in the array.

const myArray = ['a', 'b', 'c', 'c', 'b', 'd'];

const elementCounts = {};

myArray.forEach(element => {
  elementCounts[element] = (elementCounts[element] || 0) + 1;
});

console.log(elementCounts);

Output

{
  a: 1,
  b: 2,
  c: 2,
  d: 1
}

Using Reduce

The reduce() method applies the defined function to each element of the array and passes the result of each iteration to the next iteration.

To count the duplicates using the reduce() method,

  • Invoke the reduce method in the array.
  • During each iteration, increment the element count if the element already exists. Else, initialise the element count to 1.

Use this method when you want to count the duplicates in an array in JavaScript using the ES6 method.

Code

const myArray = ['a', 'b', 'c', 'c', 'b', 'd'];

var elementCounts = myArray.reduce((count, item) => (count[item] = count[item] + 1 || 1, count), {});

console.log(elementCounts);

Output

{
  a: 1,
  b: 2,
  c: 2,
  d: 1
}

This is how you can count the duplicates using the ES6 method.

Using Map and Filter

The map() method applies a function to every element in the array and creates a new array out of the results.

To count the duplicates in an array using map(),

  • Create a set with the array elements. The set will have unique elements.
  • Apply the map() method to the uniqueelements Set and use the filter() method to check how many times the element occurs in the original array.
  • You’ll get a two-dimensional array having the element and count of each element in it.

Use this method when you want to have the list of unique elements from an array and find the count of duplicates in an array.

Code

const myArray = ['a', 'b', 'c', 'c', 'b', 'd'];

let uniqueElements = [...new Set(myArray)];

const elementCounts = uniqueElements.map(value => [value, myArray.filter(str => str === value).length]);

console.log(elementCounts);

Output

[["a", 1], ["b", 2], ["c", 2], ["d", 1]]

Additional Resources

Leave a Comment