Search for most popular values in javascript array

0

How can I modify this function so that instead of showing me the most popular value, it shows me the 3 most popular values, with popular I refer to the values that are most repeated in the array.

Function

function mode(array)
{
    if(array.length == 0)
        return null;
    var modeMap = {};
    var maxEl = array[0], maxCount = 1;
    for(var i = 0; i < array.length; i++)
    {   
        var el = array[i];
        
        if(modeMap[el] == null){
            modeMap[el] = 1;6
          }else{
            modeMap[el]++;  
          }
        if(modeMap[el] > maxCount)
        { 
            maxEl = el;
            maxCount = modeMap[el];
        }
    }
    return maxEl;
}

Example of array

["PHP", "Joomla", "PHP", "PHP", "PHP", "Joomla", "PHP", "PHP", "Joomla", "Wordpress", "PHP", "PHP", "Joomla", "Joomla", "PHP", "Joomla", "PHP", "Joomla", "PHP", "Joomla", "PHP", "PHP", "PHP", "PHP", "PHP", "PHP", "NodeJS", "Joomla", "PHP"]0: "PHP"1: "Joomla"2: "PHP"3: "PHP"4: "PHP"5: "Joomla"6: "PHP"7: "PHP"8: "Joomla"9: "Wordpress"10: "PHP"11: "PHP"12: "Joomla"13: "Joomla"14: "PHP"15: "Joomla"16: "PHP"17: "Joomla"18: "PHP"19: "Joomla"20: "PHP"21: "PHP"22: "PHP"23: "PHP"24: "PHP"25: "PHP"26: "NodeJS"27: "Joomla"28: "PHP"length: 29__proto__: Array[0]
    
asked by Santiago D'Antuoni 31.03.2017 в 19:47
source

1 answer

4
  

How can I modify this function so that instead of showing me the most popular value, show me the 3 most popular values?

Instead of modifying your function I will propose a new one.

To get the result you are looking for, you could do this:

  • We create an object ( hash ) where we will keep track of the number of times the valor appears in the array.

  • With .forEach() we go through the arrangement and in the hash the account of the appearances.

  • Then with Object.keys , we obtain an arrangement with the keys of hash and with the method .sort we order the arrangement.

In this way, you will get a array with the valores ordered descending according to the number of occurrences in the array initial%.

So for example:

function mode(array) {
  var hash = {};
  // Contamos ocurrencias
  array.forEach(function(value) {
    hash[value] = hash[value] || 0;
    hash[value]++;
  });
  return Object.keys(hash)
    // Ordenamos el hash
    .sort(function(a, b) {
      return hash[b] - hash[a];
    });
}


var array = ["PHP", "Joomla", "PHP", "PHP", "PHP", "Joomla", "PHP", "PHP", "Joomla", "Wordpress", "PHP", "PHP", "Joomla", "Joomla", "PHP", "Joomla", "PHP", "Joomla", "PHP", "Joomla", "PHP", "PHP", "PHP", "PHP", "PHP", "PHP", "NodeJS", "Joomla", "PHP"];
console.log(JSON.stringify(mode(array)));
// Solo los ultimos 3
console.log(JSON.stringify(mode(array).slice(0, 3)));

// UPDATE

If you also need to know what the número de ocurrencias of each valor , you could do it like this:

  • With .reduce() we go through the arrangement and get a array of object , where each object has the properties value (value in array initial) and count (number of occurrences).

function mode(array) {
  var hash = {};
  return array
    // Contamos ocurrencias
    .reduce(function(result, value) {
      if (hash[value] === undefined) {
        hash[value] = result.length;
        result.push({
          value: value,
          count: 0
        });
      }
      result[hash[value]].count++;
      return result;
    }, [])
    // Ordenamos el hash
    .sort(function(a, b) {
      return b.count - a.count;
    });
}


var array = ["PHP", "Joomla", "PHP", "PHP", "PHP", "Joomla", "PHP", "PHP", "Joomla", "Wordpress", "PHP", "PHP", "Joomla", "Joomla", "PHP", "Joomla", "PHP", "Joomla", "PHP", "Joomla", "PHP", "PHP", "PHP", "PHP", "PHP", "PHP", "NodeJS", "Joomla", "PHP"];
console.log(JSON.stringify(mode(array)));
// Solo los ultimos 3
console.log(JSON.stringify(mode(array).slice(0, 3)));
    
answered by 31.03.2017 / 20:51
source