I am making a code that finds the mode of a set of numbers in an array (differenceArr
). I almost cracked it, except there is one problem. Let me show you my code so far so you can understand:
var mapping = {};
var counter = 0;
for(var i = 0;i < differenceArr.length; i++){
if (!mapping[differenceArr[i]]) mapping[differenceArr[i]] = 0;
mapping[differenceArr[i]] += 1;
}
var z;
var toValidateModeJSONObj = mapping;
var max_of_difarray = Math.max.apply(Math, differenceArr);
var howManyActuallyExist = -1;
var modeArray = [];
for(var n = 0; n< max_of_difarray; n++){
var exists = toValidateModeJSONObj[differenceArr[n].toString()];
if(exists == undefined){
exists = false;
}else{
howManyActuallyExist++;
modeArray[howManyActuallyExist] ={theNumber: differenceArr[n].toString(), theValue: exists};
}
console.log(JSON.stringify(modeArray));
Now that I have everything in modeArray
, I have to get which one has the biggest theValue
in modeArray
, and then I have to get the mode in a variable so I can return it. How do I do that? Anyone have any working code snippets?