Open In App

JavaScript Program to Find Second Most Repeated Word in a Sequence

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Finding the second most repeated word in a sequence involves analyzing a given text or sequence of words to determine which word appears with the second-highest frequency. This task often arises in natural language processing and text analysis. To solve it, we need to parse the input sequence, count word frequencies, and identify the word with the second-highest count.

Examples:

Input : {"1", "2", "1", "1", "2", "3"}
Output : "2"

Input : {"a", "b", "c", "a", "a", "b"}
Output : "b"

Approach 1: Using Map() in JavaScript

This approach involves iterating through the words in the sequence and using a hash map to count their frequencies. By maintaining two variables to track the most repeated and second most repeated words, we can efficiently identify the second most repeated word.

Syntax:

let frequencyMap = new Map();
frequency.sort()

Example: Below is the implementation of the above approach.

Javascript




function findSecondMostFrequentElement(arr) {
    let frequencyMap = new Map();
      
    // Counting frequency of each element
    for (let i = 0; i < arr.length; i++) {
        if (frequencyMap.has(arr[i])) {
            frequencyMap.set(arr[i],
                frequencyMap.get(arr[i]) + 1);
        } else {
            frequencyMap.set(arr[i], 1);
        }
    }
    let maxFrequency = Number.MIN_SAFE_INTEGER;
    let frequencies = [];
  
    // Finding the maximum frequency
    for (let [key, value] of frequencyMap) {
        if (value > maxFrequency) {
            maxFrequency = value;
        }
    }
  
    for (let [key, value] of frequencyMap) {
        if (value !== maxFrequency) {
            frequencies.push(value);
        }
    }
    frequencies.sort((a, b) => a - b);
  
    // Returning the second most frequent element
    for (let [key, value] of frequencyMap) {
        if (value ===
            frequencies[frequencies.length - 1]) {
            return key;
        }
    }
  
    return "-1";
}
  
let arr = ["1", "2", "3", "1", "1", "2"];
let ans = findSecondMostFrequentElement(arr);
console.log(ans);


Output

2

Time Complexity : O(Nlog(N))

Approach 2: Using Set() in JavaScript

The approach is to count the occurrences of each word in the array using a Map, then find the second most repeated word by comparing the occurrence counts. It iterates through the array to count word occurrences and efficiently identifies the second most repeated word using a Map data structure.

Syntax:

let occurrences = new Map();
occurrences.set()

Example: Below is the implementation of the above approach.

Javascript




// Function to find the second most repeated word
function findSecondMostRepeatedWordInArray(words) {
  
    // Store all the words with their occurrences
    let occurrences = new Map();
    for (let i = 0; i < words.length; i++) {
        if (occurrences.has(words[i])) {
            occurrences.set(words[i],
                occurrences.get(words[i]) + 1);
        } else {
            occurrences.set(words[i], 1);
        }
    }
      
    // Find the second largest occurrence
    let firstMax =
        Number.MIN_VALUE, 
            secondMax = Number.MIN_VALUE;
    for (let [key, value] of occurrences) {
        if (value > firstMax) {
            secondMax = firstMax;
            firstMax = value;
        } else if (value > secondMax 
                && value !== firstMax) {
            secondMax = value;
        }
    }
      
    // Return the word with 
    // an occurrence equal to secondMax
    for (let [key, value] of occurrences) {
        if (value === secondMax) {
            return key;
        }
    }
}
  
// Driver program
let wordsArray = ["a", "b", "c", "a", "a", "b"];
console.log(findSecondMostRepeatedWordInArray(wordsArray));


Output

b

Time Complexity: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads