-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise4_modus.js
37 lines (33 loc) · 957 Bytes
/
exercise4_modus.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
function cariModus(arr) {
let frekuensiTerbanyak = 0;
let modus = 0;
for (let i=0; i<=arr.length-1; i++){
let frekuensi = 0;
for (let j=i; j<=arr.length-1; j++){
if (arr[i]===arr[j] && i!==j){
frekuensi++
if (frekuensi>frekuensiTerbanyak){
frekuensiTerbanyak = frekuensi
modus = arr[i];
}
}
}
}
if (frekuensiTerbanyak===0){
return -1
}
let totalArray = 0;
for (let k=0; k<=arr.length-1; k++){
totalArray+=arr[k]
}
if (totalArray/arr[0]==arr.length){
return -1
}
return modus
}
// TEST CASES
console.log(cariModus([10, 4, 5, 2, 4])); // 4
console.log(cariModus([5, 10, 10, 6, 5])); // 5
console.log(cariModus([10, 3, 1, 2, 5])); // -1
console.log(cariModus([1, 2, 3, 3, 4, 5])); // 3
console.log(cariModus([7, 7, 7, 7, 7])); // -1