Photo by Ryan Quintal on Unsplash
In this article, i am going to share information about how to calculate the repeated word count in the given array.
Repetition count calculation method.
const getRepeatedWordDetails = (inputArray) => {
// extract the unique items
const uniqueItems = [...new Set(inputArray)];
const repetationObj = {};
// repetation item count calculation
uniqueItems.forEach(item => {
const filtereItem = inputArray.filter(word => word === item);
repetationObj[item] = filtereItem.length;
});
return repetationObj;
}
Count from Array of Strings
const stringArray = ["react", "js", "react", "angular", "react", "js"];
getRepeatedWordDetails(stringArray);
Output
{react: 3, js: 2, angular: 1}
Count from Array of Integers
const arr2 = [10, 20, 20, 30, 10, 20, 30];
getRepeatedWordDetails(arr2);
Output
{10: 2, 20: 3, 30: 2}
That’s it for this post friends. I have tried to find the repeated word count in this way if you guys have any other idea share with me will attempt to learn together.