-
Notifications
You must be signed in to change notification settings - Fork 1
/
22 - Mean Mode.js
74 lines (64 loc) · 2.06 KB
/
22 - Mean Mode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Step By Step
function MeanMode (arr) {
// First, we declare an empty object to hold the number of times each item in the input array occurs,
var modeMap = {};
// a variable to hold the actual mode,
var mode = 0;
// a variable to hold the number of times the mode occurs,
var modeCount = 0;
// and a variable to hold the sum of the items in the array (to calculate the mean),
var sum = 0;
// Next, we loop through each item in the input array and...
for (var i = 0; i < arr.length; i++) {
// ...add each value to our sum variable
sum += arr[i];
// ...check if each value is present in our frequency count
if (modeMap[arr[i]]) {
// If it is, we increase its value by 1.
modeMap[arr[i]]++;
} else {
// If it isn't, we initialize its value to 1.
modeMap[arr[i]] = 1;
}
// ...and check if the count is greater than our current mode's count.
if (modeMap[arr[i]] > modeCount) {
// If it is, we set the mode to the current item in our input array...
mode = arr[i];
// ...and set the number of times the mode has occured to the current item's value in our mode array.
modeCount = modeMap[arr[i]];
}
}
// Next, we calculate the mean by dividing the sum by the total number of items in the array.
var mean = sum / arr.length;
// If the mean and the mode are the same, we return 1, otherwise we return 0.
if (mean === mode) {
return 1;
} else {
return 0;
}
}
// Without Comments
function MeanMode (arr) {
var modeMap = {};
var mode = 0;
var modeCount = 0;
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
if (modeMap[arr[i]]) {
modeMap[arr[i]]++;
} else {
modeMap[arr[i]] = 1;
}
if (modeMap[arr[i]] > modeCount) {
mode = arr[i];
modeCount = modeMap[arr[i]];
}
}
var mean = sum / arr.length;
if (mean === mode) {
return 1;
} else {
return 0;
}
}