-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMode.js
More file actions
28 lines (22 loc) · 734 Bytes
/
findMode.js
File metadata and controls
28 lines (22 loc) · 734 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
27
/**
* -------------------------------------------------------
* Programming Question : Find Mode
* -------------------------------------------------------
**/
// Q. Write a JavaScript function called findMode that takes anarray of numbers as input and returns the mode of the array (the number that appears most frequently).
function findMode(arr) {
let maxNum=0;
let mode;
arr = arr.sort((a,b) => a-b);
let occurance = arr.reduce((accm, currn) => {
accm[currn] = (accm[currn] || 0) + 1;
if(accm[currn]>maxNum){
maxNum = accm[currn];
mode = maxNum;
}
return accm;
},{})
console.log(occurance);
return mode;
}
console.log(findMode([3, 2, 1, 5, 4, 3, 2, 3]));