Sort by

recency

|

1927 Discussions

|

  • + 0 comments
      public static int equalizeArray(List<Integer> arr) {
        HashMap<Integer, Integer> ele = new HashMap<>();
        for(int n : arr)
           ele.put(n, Collections.frequency(arr, n));
        return arr.size() - Collections.max(ele.values());  
        }
    
  • + 0 comments
    function equalizeArray(arr) {
        let freq = {}; 
        
        for(let n of arr) {
            freq[n] = (freq[n] || 0 ) + 1; 
        }
        
        let maxFreq = Math.max(...Object.values(freq)); 
        
        return arr.length - maxFreq; 
    }
    // Be Happy and Code every day !!
    
  • + 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())