I was studying the function in the answer below:
Get the element with the highest occurrence in an array
but I am saddened to see it doesn't work for me (on repl.it). All the values end up being NaN, no matter how I try to increment them on line 13. What's wrong?
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] = modeMap[el] + 1;
if(modeMap[el] > maxCount)
{
maxEl = el;
maxCount = modeMap[el];
}
}
console.log(modeMap)
return maxEl;
}
mode([4,2,6,2,6,6,6,6,]);