6

I'm trying to get the most popular/common word from an Array, I've tried the following but instead of it just saying 'Rain' it displays this inside the console instead [rain: 2, hot: 1].

What am I doing wrong? I only want to display the number one most popular words without the number.

Any help/advice would be helpful, thank you.

  var defaultArray = [{age:"25-35",country:"united kingdom",sex:"male",word:"rain"},{age:"25-35",country:"united arab emirates",sex:"male",word:"hot"},{age:"25-35",country:"zimbabwe",sex:"female",word:"rain"}];

  /* Popular */
  function popular() {
    var words = [];
    for(var p = 0; p < defaultArray.length; p++){
      var word = defaultArray[p].word;
      words.push(word);
      console.log(words);
    }
    var count = [];
    for (var i = 0, j = words.length; i < j; i++) {
      if (count[words[i]]) {
        count[words[i]]++;
      }
      else {
        count[words[i]] = 1;
      }
    }
    console.log(count);
  }
  popular();
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35

5 Answers5

5

You just need to loop through the count array and find the word with the highest count:

var mostCommonWordCount = 0;
var mostCommonWord = "";
for (var key in count) {
    if (count[key] > mostCommonWordCount) {
        mostCommonWordCount = count[key]
        mostCommonWord = key;
    }
}
console.log(mostCommonWord);

After this runs, mostCommonWord will contain the word with the highest count. The only caveat is that if there are multiple words with 3 occurrences, for example, then the first one in the count array is the one that will be assigned to mostCommonWord. So if you're wanting something that will return multiple words if they all have the same count, you'd need to modify this a bit.

Disclaimer: There is likely a better way to do this, but this is the first thing I came up with.

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
1

in keeping with the original functionality you had. you could just tweak it a bit to get the most popular word. NB: this does not account for ties.

var defaultArray = [
  {age:"25-35",country:"united arab emirates",sex:"male",word:"hot"},
  {age:"25-35",country:"united kingdom",sex:"male",word:"rain"},
  {age:"25-35",country:"united arabemirates",sex:"male",word:"hot"}];

/* Popular */
function popular() {
  var count = 0
  var max=0;
  var currentword="";
  var words = [];

  for(var p = 0; p < defaultArray.length; p++){
    var word = defaultArray[p].word;
    words.push(word);
  }

  for (var i = 0; i<words.length; i++) {
    if (max<=count){
      max=count;    
    }
    count=0;
    for (var x=0; x<words.length; x++)
    {
      if (words[i]==words[x] ){
        count++;
      }
      if (count>max)currentword=words[i];
    }
  }
  console.log(currentword);
} 
david
  • 3,225
  • 9
  • 30
  • 43
0

You can remove almost everything after you create an array of words and instead add this function:

function mostPopularWord(arr){
  return arr.sort(function(a,b){
    return arr.filter(function(v){ return v===a }).length -
        arr.filter(function(v){ return v===b }).length
  }).pop();
}

Then pass it your array of words like:

mostPopularWord(words) // Returns rain

Here is a jsFiddle

I did not write that function, I found it when searching for a way to help. You can read more about what you were trying to do here: Get the element with the highest occurrence in an array

david
  • 3,225
  • 9
  • 30
  • 43
Jeramiah Harland
  • 854
  • 7
  • 15
0

for counting and groupping you can use reduce function, like this:

var defaultArray = [{
    age: "25-35",
    country: "united kingdom",
    sex: "male",
    word: "rain"
  }, {
    age: "25-35",
    country: "united arab emirates",
    sex: "male",
    word: "hot"
  }, {
    age: "25-35",
    country: "zimbabwe",
    sex: "female",
    word: "rain"
  }];

  /* Popular */
  function popular(arr) {
    return arr.reduce(function(acc, cur) {
      if (!acc[cur.word]) acc[cur.word] = 0;
      acc[cur.word] += 1;
      if (acc.MaxCount < acc[cur.word]) {
        acc.MaxCount = acc[cur.word];
        acc.PopularWord = cur.word;
      }
      return acc;
    }, {
      MaxCount: 0
    }).PopularWord;
  }
  document.write('Max popular word: ',popular(defaultArray));
Grundy
  • 13,356
  • 3
  • 35
  • 55
0

I would highly recommend a JavaScript utility library like lodash, in which case you could do something like this:

var obj = _.countBy(_.map(defaultArray, 'word'));
console.log(obj); // Object {rain: 2, hot: 1}

var word = _.keys(obj).reduce(function (a,b) { return obj[a] > obj[b] ? a : b });
console.log(word); // "rain"

Note: See other answers for plain JavaScript functionality

Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37