1

I have an array that looks like this:

newcount = [
    nomad,
    explorer,
    nomad,
    ship,
    explorer,
    explorer,
    nomad,
    nomad
];

How would I use javascript to loop through this array and return the word that appears the most? (in my case--nomad)

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
user1836025
  • 209
  • 4
  • 14
  • 2
    Make an object whose keys are the words, values are the count. Loop through the array, adding the keys to the object or incrementing the count. Then go through the object looking for the highest count. – Barmar Jul 24 '13 at 18:54
  • That are lots of questions like this one, for example: http://stackoverflow.com/questions/2440295/extracting-the-most-duplicate-value-from-an-array-in-javascript-with-jquery – Ygor Cardoso Jul 24 '13 at 18:55

4 Answers4

2

You should use a hash map.

Like this:

var map = {};
for (var i = 0; i < newcount.length; i++) {
    if (!map[newcount[i]]) {
        map[newcount[i]] = 1;
    } else {
        map[newcount[i]]++;
    }
}

When you've finished it, you can query the word count for any given word on the map. For example:

map["nomad"]; // evaluates to 4 in your case.

It's pretty easy to fetch the one that appears the most now. Just check every member of the map.

For example:

var appearsMost = "";
var greatestValue = 0;

for (var foo in map) {
    if (map[foo] > greatestValue) {
        greatestValue = map[foo];
        appearsMost = foo;
    }
}

Now just check the value of appearsMost.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
0

You can try this code :

    var newcount = ['nomad','explorer','nomad','ship','explorer','explorer','nomad','nomad'];

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;
        else
            modeMap[el]++;  
        if(modeMap[el] > maxCount)
        {
            maxEl = el;
            maxCount = modeMap[el];
        }
    }
    return maxEl;
}

alert(mode(newcount));

This code was given in this page : Get the element with the highest occurrence in an array

Community
  • 1
  • 1
Junior Dussouillez
  • 2,327
  • 3
  • 30
  • 39
0

Another option is to extend the array prototype with a count method;

Array.prototype.count = function(obj){
    var count = this.length;
    if(typeof(obj) !== "undefined"){
        var array = this.slice(0), count = 0; // clone array and reset count
        for(i = 0; i < array.length; i++){
            if(array[i] == obj){
                count++;
            }
        }
    }
    return count;
}

Usage;

newcount.count("nomad"); // 4
GriffLab
  • 2,076
  • 3
  • 20
  • 21
0
function recurrent(arr) {
    var count = {},
        highest = 0,
        ret = null;
    for (var i = 0; i < arr.length; i++) {
        var curr = arr[i];
        count[curr] = typeof count[curr] === "undefined" ? 1 : count[curr] + 1;
    }
    for (var name in count) {
        if (!count.hasOwnProperty(name)) continue;
        var currCount = count[name];
        if (currCount > highest) {
            highest = currCount;
            ret = name;
        }
    }
    return ret;
}

Usage(here you find the jsfiddle):

var a = [0, 0, 0, 1, 1, 2, 3, 4, 5, 5, 5, 5, 5, 6];
console.log(recurrent(a)); // 5

Explanation:

count[curr] = typeof count[curr] === "undefined" ? 1 : count[curr] + 1;

With this line we initialize(or increase if already set) the count of that element in the array so that we can use it later to see which element appears the most.

if (currCount > highest) {
    highest = currCount;
    ret = name;
}

If the count of the current element is greater than the highest counted until now, this block updates the highest value with the current one and set the return name to the current one.

Niccolò Campolungo
  • 11,824
  • 4
  • 32
  • 39