How to find the most frequent word in an array of strings in JS

When building a program, we may need to find the most frequent word in a list of words. In JavaScript, there are multiple approaches to achieve this. Here, we will discuss a simple method.

In this method, we will do the following steps:

  1. Collect the unique words present in an array

  2. Sort the words into groups

  3. Determine the count of each unique word

Now, let’s move on to the first step of our method, which involves collecting unique words from the array.

Collecting unique words

This step is necessary to simplify finding the count of individual words later.

const words = ['Build', 'Debug', 'Build', 'Deploy', 'Test', 'Build'];
// Extract unique words
const uniqueWords = new Set(words);
console.log(uniqueWords);
// Expected Output: Set {'Build', 'Debug', 'Deploy', 'Test'};

Using JavaScript’s new keyword with the Set() constructor, we create a set object from our array. A set is used to hold a collection of unique values. Every word in the array will appear only once in our set.

Sorting into groups

We can rearrange the elements in our original array alphabetically so that identical words are grouped next to each other. In the next step, we will compare the starting and ending positions of each word group in the sorted array to determine the frequency of each word. JavaScript has several helpful methods built into arrays to enable working on them. We use one of those methods, sort(), below, to rearrange the words.

const words = ['Build', 'Debug', 'Build', 'Deploy', 'Test', 'Build'];
console.log('Initial:', words);
words.sort();
console.log('Sorted:', words);
// Expected Output: [ 'Build', 'Build', 'Build', 'Debug', 'Deploy', 'Test' ]"

Determining the count of words

With our set of unique words, uniqueWords, and the words array of grouped words, we can go through one unique word after the other, and determine how many times each occurs in the words array. Below, we do that with the forEach() method. It executes a callback function for the words, one after the other.

const words = ['Build', 'Debug', 'Build', 'Deploy', 'Test', 'Build'];
// Sort the array
words.sort();
console.log(words);
// Extract unique words
const uniqueWords = new Set(words);
uniqueWords.forEach((word) => {
const start = words.indexOf(word);
const end = words.lastIndexOf(word);
const count = end - start + 1;
console.log(word, ':', count);
});

In lines 11 and 12 of the callback, using two more built-in array methods, we got the start and end position of the group of the current word. indexOf() and lastIndexOf() both take a word and return a number representing the index position where it appears first and last, respectively. We use these indexes to calculate the word count.

Let’s understand the logic of word counts with an example. In the array below, “C” appears three times. It starts at the index position 2 and ends at 4. We can find the count by subtracting 2 from 4, then adding 1.

Counting in the array (4 - 2 + 1 = 3)
Counting in the array (4 - 2 + 1 = 3)

The reason for adding 1 after the subtraction is to get the actual count for each word. The result of the subtraction only considers index 2 and index 3 of the word “C,” so a “1” is added to compensate.

Putting it together

Here’s everything we’ve done so far, put in the function findMostFrequentWord. Let’s run the following code to get the most frequent word in an array of strings.

function findMostFrequentWord(words) {
// Extract unique words
const uniqueWords = new Set(words);
// Sort words
words.sort()
let highestCount = 0;
let mostFrequent = [];
uniqueWords.forEach((word) => {
// Count word
const start = words.indexOf(word);
const end = words.lastIndexOf(word);
const count = end - start + 1;
// Update highest count and most frequent word
if (count > highestCount) {
highestCount = count;
mostFrequent = [word];
}
// Add word to most frequent list if it has
// the same count as the current highest
if (count === highestCount && !mostFrequent.includes(word)) {
mostFrequent.push(word);
}
});
return mostFrequent;
}
const words = ['Build', 'Debug', 'Build', 'Deploy', 'Test', 'Build'];
const mostFrequentWord = findMostFrequentWord(words);
console.log(mostFrequentWord);

We’ve added two variables: highestCount, which keeps track of the highest count as we go through the words, and mostFrequent, to hold the most frequent word(s).

Further down, we added two conditional statements on lines 17 and 23. The first compares the word count to the highest count so far, and updates our variables if it is greater. In case multiple words have the highest count, the second statement checks if the current word has the same count, then includes it accordingly.

Free Resources