Sort by

recency

|

1925 Discussions

|

  • + 0 comments

    Here is my O(N) c++ solution, you can watch the explanation here : https://youtu.be/8p9yuqSv4ek

    int equalizeArray(vector<int> arr) {
        map<int, int>mp;
        int mx = 0;
        for(int el: arr) {
            mp[el]++;
            if(mp[el]>mx) mx = mp[el];
        }
        return arr.size() - mx;
    }
    
  • + 0 comments

    Python solution with O(n) complexity and without using collections library:

    def equalize_array(arr):
        count = {}
        for el in arr:
            if el in count:
                count[el] += 1
            else:
                count[el] = 1
        # Find the maximum frequency
        max_count = 0
        for value in count.values():
            if value > max_count:
                max_count = value
        # Calculate the number of elements to remove
        return len(arr) - max_count
    
  • + 0 comments
    def equalizeArray(arr):
        d=dict(Counter(arr))
        return len(arr) - max(d.values())
    
  • + 0 comments

    include

    using namespace std; int test(int n, vector a){ unordered_map v; for(int it : a){ v[it]++; } int max_value = 0; for(auto& pair : v){ max_value = max(max_value, pair.second); } return n - max_value; } int main(){ int n; cin >> n; vector a(n); for(int i = 0; i < n; i++){ cin >> a[i]; } cout << test(n, a) << endl; return 0; }

  • + 0 comments

    PHP

    function equalizeArray($arr) {
        $counter = [];
        $max = [];
        foreach ($arr as $el) {
            isset($counter[$el]) ? $counter[$el]++ : $counter[$el] = 1;
            if ($counter[$el] > $max['value']) {
                $max['value'] = $counter[$el];
                $max['index'] = $el;
            }
        }
        
        unset($counter[$max['index']]);
        return array_sum($counter);
    }