HackerRank: Equalize the Array
Tags: hackerrank, 紮馬步
Equalize the Array
-
慢慢養把 pseudocode 的fu 找回來~ 紙本手寫是我最愛的好夥伴:herat:
-
python
def equalizeArray(arr):
max_num = max(arr, key=arr.count)
delection_count = 0
for item in arr:
if item != max_num:
delection_count += 1
return delection_count
-
javascript
- 拉屎完後認真想 有沒有辦法只做一次 loop 來解
- https://medium.com/@AmJustSam/how-to-find-most-frequent-item-of-an-array-12015df68c65
// Complete the equalizeArray function below.
function equalizeArray(arr) {
let counter_dict = {};
let most_occur_num = - Infinity;
let mini_del = -Infinity;
for (let i=0; i <= arr.length; i ++ ) {
let el = arr[i];
if (counter_dict[el] === undefined) {
counter_dict[el] = 1;
} else {
counter_dict[el] += 1;
};
if (most_occur_num < counter_dict[el]) {
most_occur_num = counter_dict[el]
mini_del = arr.length - most_occur_num
}
}
return mini_del;
}