How To Count The Duplicates In An Array Using JavaScript?

Count the duplicates in an array using JavaScript

There are many methods to count the duplicates in an array using Javascript. You can try using reduce() or forEach() loop method. Read on this article now to get more knowledge.

Count the duplicates in an array using JavaScript

The idea for counting the duplicates in an array is that we have to create an object. We will return each value of the array into a key of that object, then we’ll have a number indicating how many times that element is within the array.

The result will be an object with properties: The key will be the value of the array, and the number following will be the count. 

Using forEach() method 

The forEach() method is a method of arrays that allows us to go through each array element and apply that element to a function we pass in.

Syntax:

array.forEach((element) => {...})

Parameters:

  • element: The current element is being processed in the array.

For example:

let array = ["Mon", "Sun", "Wed", "Mon", "Fri", "Sun", "Wed", "Mon"];

We have an array of many elements and duplicate elements many times.

let eCount = {};

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

The first thing we set up is an empty object, and then we will iterate through the array by forEach loop. 

We encounter new values that we haven’t encountered before. We will make them a new property in the object and add a count.

We check if the current element is already set as a key of the object. If yes, we will increment it by 1. If it is not set yet, we initialize it to 1.

Code:

let array = ["Mon", "Sun", "Wed", "Mon", "Fri", "Sun", "Wed", "Mon"];
let eCount = {};

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

console.log(eCount);

Output:

Using reduce() method

In JavaScript, the array.reduce() method is used to reduce an array to a single value and execute the provided function for each value of the array. The function’s return value stored in the accumulator.

Syntax:

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

Parameters:

  • total: The initial value, or the previously returned value of the function (required)
  • currentValue: The value of the current element (required)
  • currentIndex: The index of the current element (optional)
  • arr: Specify the array object to which the current element belongs (optional)
  • initialValue: Specify the value to be passed to the function as the initial value (optional)

The idea of ​​this method is the same as the forEach() method:

let array = ["Mon", "Sun", "Wed", "Mon", "Fri", "Sun", "Wed", "Mon"];

let eCount = array.reduce((accVal, val) => {
	accVal[val] = (accVal[val] || 0) + 1;
	return accVal;
}, {});

console.log(eCount);

Output:

Summary

Above, I’ve shown you how to count the duplicates in an array using JavaScript by two methods: forEach() method and reduce() method. To better understand the lesson’s content, practice rewriting today’s examples. And let’s learn more about JavaScript in the next lessons here.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *