kanika mittal
kanika mittal

Reputation: 15

how to find the most repeat word in string?

can you please tell me how to find the most repeat word in string ? Example If input is this "how do you do" Output is "do"

var str="how do you do"
function findMostReaptedWord(str){
    var res = str.split(" ");
    alert(res.length);
    var count;
    var compareString;
    for(var i=0;i<res.length ;i++){
        count=0;
        compareString=res[i]
        for (j=0;i<res.lenth ;j++){
            if(compareString==res[j]){
                count++
            }
        }

    }

}

alert(findMostReaptedWord(str))

fiddle http://jsfiddle.net/omjg9v0q/

Upvotes: 1

Views: 6918

Answers (4)

MD. Sakib Khan
MD. Sakib Khan

Reputation: 41

Here is my approach

  • First, separate the words from the string using Regular Expression.
  • Declare an object as a Map which will help you to find the occurrences of each word. (You can use Map Data Structure!)
  • Find the most repeated word from that object.

let str = 'How do you do?';
console.log(findMostRepeatedWord(str)); // Result: "do"

function findMostRepeatedWord(str) {
  let words = str.match(/\w+/g);
  console.log(words); // [ 'How', 'do', 'you', 'do' ]

  let occurances = {};

  for (let word of words) {
    if (occurances[word]) {
      occurances[word]++;
    } else {
      occurances[word] = 1;
    }
  }

  console.log(occurances); // { How: 1, do: 2, you: 1 }

  let max = 0;
  let mostRepeatedWord = '';

  for (let word of words) {
    if (occurances[word] > max) {
      max = occurances[word];
      mostRepeatedWord = word;
    }
  }

  return mostRepeatedWord;
}

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382160

I gave the idea in a comment. Here it is in code :

function findMostReaptedWord(str){
    var counts = {}, mr, mc;
    str.match(/\w+/g).forEach(function(w){ counts[w]=(counts[w]||0)+1 });
    for (var w in counts) {
      if (!(counts[w]<mc)) {
        mc = counts[w];
        mr = w;
      }
    }
    return mr;
}

A few details :

  • I use str.match(/\w+/g) for a better decomposition in words. Yours would take anything not a space as a word or part of a word.
  • counts is a map giving the number of occurrences of each words (i.e. counts["do"] is 2)
  • using a map avoids doing two levels of loop, which is very slow

Upvotes: 3

Mebin
Mebin

Reputation: 109

Here I give you an approach,

  1. Sort the words first. That way "how do you do" becomes "do do how you".
  2. Iterate the string to count the words that repeat, keep the maximum number of times repeating word in memory while iterating.

- Mebin

Upvotes: 0

Khalid
Khalid

Reputation: 4808

This function may help you

function maxWord (str)
{
    var max = 0;
    var maxword = '';
    var words = str.split(' ');
    for(i=0;i<words.length;i++)
    {
        var count = 0;
        var word = '';
        for(j=0;j<words.length;j++)
        {
            if(j !== i && words[i] === words[j])
            {
                count++;
                word = words[i];
            }
        }
        if(count>maxword)
        {
            max = count;
            maxword = word;
        }
    }
    return maxword;
}
maxWord('how do you do'); // returns do

Upvotes: -1

Related Questions