2

I am having some problems how to actually get the biggest value of the map I have gotten. Right now, my code only displays the actual counted values of each key, I am stuck at trying to log the max, what I think happens in my code, is the for loop goes through the counts[key] array, but it will remain at 1, which is the first value and stops, because the only number that is logged is 1. I am not expecting the actual answer, maybe some tips and hints that would lead me the right way. Thanks in advance.

var Mode = function(data) {
    var counts = {};
    for (let i = 0; i < data.length; i++) {
        counts[data[i]] = (counts[data[i]] + 1) || 1;
    }
    for (var key in counts) {
        if (counts.hasOwnProperty(key)) {
            var maxValue = Math.max(counts[key]);
        }
    }
    return maxValue;
}
console.log(Mode([1, 5, 2, 3, 3, 4, 4, 4]));
Siegfried
  • 41
  • 7
  • What is the correct answer? 4 or 5? – Krzysztof Safjanowski Oct 20 '16 at 10:17
  • the correct answer would be 3, since my code actually counts each of the array value from data, so basically it's '1' = 1; '2' = 2; '3' = 2; '4' = 3; '5' = 1; What I'm actually looking for, is to compare the actual counted numbers of the data array, which is why I put all of those into a map and now I'm trying to compare each of the values and log the right answer – Siegfried Oct 20 '16 at 10:21
  • 1
    But `4` has the highest count in your example. – Walf Oct 20 '16 at 10:44
  • Possible duplicate of [Get the element with the highest occurrence in an array](http://stackoverflow.com/questions/1053843/get-the-element-with-the-highest-occurrence-in-an-array) – Krzysztof Safjanowski Oct 20 '16 at 11:01

3 Answers3

1

You need ah higher number of count in this array.try this

var max =0;
var Mode = function(data) {
    var counts = {};
    for (let i = 0; i < data.length; i++) {
        counts[data[i]] = (counts[data[i]] + 1) || 1;
    }
    for (var key in counts) {
        if (counts.hasOwnProperty(key)) {
             if(counts[key] > max){max=counts[key];}
          
        }
    }
    return max;
}
console.log(Mode([1, 5, 2, 3, 3, 4,4, 4, 4]));//4 is a higher count
console.log(Mode([ 5,5,5,5,5,5,5, 2, 3, 3, 4, 4]));//5 is higher count
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • 1
    Just a comment... it's convention to start functions with a higher case first letter with "new Mode()". Just passing comment :) – Lee Brindley Oct 20 '16 at 10:31
1

I have finally solved this problem by using the following code:

var Mode = function(data) {
var counts = {};
for (let i = 0; i < data.length; i++) {
    counts[data[i]] = (counts[data[i]] || 0) + 1
}
var max = 0;
var values = [];
for (var key in counts) {
    if (counts.hasOwnProperty(key)) {
        if (counts[key] > max) {
            max = counts[key];
            values = [key];
        } else if (counts[key] === max) {
            max = counts[key];
            values.push(key);
        }
    }
}
return "The highest value is " + values.join(", ") + " with a count of " + max;
}
console.log(Mode([1, 2, 3, 3, 4, 4]));

Thanks a lot for your help :).

Siegfried
  • 41
  • 7
0
function mode(numbers) {
    // as result can be bimodal or multi-modal,
    // the returned result is provided as an array
    // mode of [3, 5, 4, 4, 1, 1, 2, 3] = [1, 3, 4]
    var modes = [], count = [], i, number, maxIndex = 0;
 
    for (i = 0; i < numbers.length; i += 1) {
        number = numbers[i];
        count[number] = (count[number] || 0) + 1;
        if (count[number] > maxIndex) {
            maxIndex = count[number];
        }
    }
 
    for (i in count)
        if (count.hasOwnProperty(i)) {
            if (count[i] === maxIndex) {
                modes.push(Number(i));
            }
        }
 
    return modes;
}

console.log(mode([1, 2, 4, 1, 4, 4]));   // 4
console.log(mode([1, 20, 3, 20, 4, 20])); // 20
Shashwat Gupta
  • 5,071
  • 41
  • 33