forked from ChristianNally/web-2023-Feb-06-west-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq3.js
More file actions
59 lines (51 loc) · 1.27 KB
/
q3.js
File metadata and controls
59 lines (51 loc) · 1.27 KB
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
/* Question 3
*
* Implement the 'mode' function defined below
*
* MODE - the most frequently occurring number
* - for this test, the provided lists will only have a single value for the mode
*
* For example:
*
* mode([6,2,3,4,9,6,1,0,5]);
*
* Returns:
*
* 6
*/
const mode = function(arr) {
// 1. create the piles
// put aside a place in memory to hold our "piles"
// { '6': 3, '1': 1, '5': 2, '4': 1 }
const piles = {};
// look at each element in the array
for (const number of arr) {
// have we seen this value before?
if (piles[number]) {
// if yes, increment the number of times seen
piles[number] += 1;
} else {
// if no, create a new "pile"
piles[number] = 1;
}
}
// log the "piles"
// console.log(piles);
// 2. compare the piles
let highestValue = 0;
let highestKey = null;
// loop through our piles
for (const key in piles) {
const value = piles[key];
// is this the tallest pile we've seen so far?
if (value > highestValue) {
// if yes, replace the highestValue and highestKey
highestValue = value;
highestKey = key;
}
}
// return the key assoc with the highest value
return Number(highestKey);
};
// Don't change below:
module.exports = { mode };