forked from DaviPRocha/hacker-rank-resolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEqualize the Array.js
26 lines (26 loc) · 888 Bytes
/
Equalize the Array.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
function equalizeArray(arr) {
// descobrir qtos numeros tem de cada
const number_qnts = {}
let maxOccurencyNumber
let leaveNumbers = 0
for (let i = 0; i < arr.length; i++) {
if (!(arr[i] in number_qnts)) {
number_qnts[arr[i]] = 0
}
number_qnts[arr[i]]++
// guardar o número q tem mais ocorrências no array
if (!maxOccurencyNumber || number_qnts[arr[i]] > number_qnts[maxOccurencyNumber]) {
maxOccurencyNumber = arr[i]
}
}
// contar os números q são diferentes do de maior ocorrência
for (let i = 0; i < arr.length; i++) {
if (arr[i] != maxOccurencyNumber) {
leaveNumbers++
}
}
// retornar essa contagem
console.log(leaveNumbers)
return leaveNumbers
}
equalizeArray([4, 3, 7, 9 ,20, 14, 2, 3, 3, 7, 18, 18, 1, 9, 9, 2, 7, 6, 12, 19, 4])