-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay24.js
More file actions
26 lines (23 loc) · 863 Bytes
/
Day24.js
File metadata and controls
26 lines (23 loc) · 863 Bytes
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
//* Coding Challenge: Find the Mode in an Array
//*•
//? Write a function called findMode that takes an array of numbers as input and returns the mode of the array (the number that appears most frequently).
function findMode(arr) {
let counts = {};
let maxSum = 0;
let repeatingElement;
for(let element of arr) {
counts[element] = (counts[element] || 0) + 1;
if(counts[element]>maxSum) {
maxSum = counts[element];
repeatingElement = element;
}
}
console.log(counts);
return repeatingElement;
console.log(`Maximum element occur ${maxSum} times`);
}
// Example usage:
console.log(findMode([1, 2, 2, 3, 1,1,1,1,1, 4, 21])); // Output: 2
//* Constraints:
//? The input array will always contain at least one element.
//? The mode will always be unique (i.e., there won't be multiple numbers with the same highest frequency).