-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode.js
More file actions
40 lines (34 loc) · 1.01 KB
/
Copy pathmode.js
File metadata and controls
40 lines (34 loc) · 1.01 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
const input = require("./input");
(async () => {
const numbers = await input("Enter numbers (Separated by Comma): ");
const num_list = numbers.split(",").map(Number);
// Count occurrences
let instancesCount = {};
num_list.forEach(num => {
if (instancesCount[num]) {
instancesCount[num] += 1;
} else {
instancesCount[num] = 1;
}
});
// Display counts
console.log("Count of all numbers:");
for (let [num, count] of Object.entries(instancesCount)) {
console.log(`${num}: ${count}`);
}
// Find the max count
let maxCount = 0;
for (let num in instancesCount) {
if (instancesCount[num] > maxCount) {
maxCount = instancesCount[num];
}
}
// Collect all modes
let modes = [];
for (let num in instancesCount) {
if (instancesCount[num] === maxCount) {
modes[modes.length] = num; // push manually
}
}
console.log(`Mode(s): ${modes.join(", ")}`);
})();