less than 1 minute read

Tags: ,

Equalize the Array

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
// 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;
}