Skip to content
Skip to content
Articles

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

Mar 10, 2023 5 min Taofeeq H. JavaScript
How to find the most frequent word in an array of strings in JS

Introduction

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 complete 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.

Collecting Unique Words

This step is necessary to simplify finding the count of individual words later. 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.

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'};

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. We use the sort() method 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.

const words = ["Build", "Debug", "Build", "Deploy", "Test", "Build"];

// Sort the array
words.sort();

// 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);
});

Using two 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.

A
A
B
B
C
C
C
C
C
C
D
D
E
E
0
0
1
1
2
2
3
3
4
4
5
5
6
6
Text is not SVG - cannot display

We're adding 1 after the subtraction because arrays are zero-indexed. This means the first element is at index 0, not 1 as you might expect. If we didn't add 1, we'd get 2 instead of 3, so 1 is added to compensate.

Want to learn more JavaScript tricks?

I frequently share JavaScript tips and technical content.

View my GitHub

Putting It All 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); // Output: ['Build']

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).

We also added conditional statements that compare the word count to the highest count so far, and update our variables if it is greater. In case multiple words have the highest count, it checks if the current word has the same count, then includes it accordingly.


First shared on Educative Answers.