Sort by

recency

|

1930 Discussions

|

  • + 0 comments

    JS solution :

    function equalizeArray(arr) {
        return arr.length - Math.max(...[...new Set(arr)].map((num, i) => arr.reduce((acc, next) => next == num ? acc + 1 : acc, 0)))
    }
    
  • + 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;
    }
    
  • + 1 comment

    java 8

    public static int equalizeArray(List arr) { int max = 0; // Write your code here

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int el : arr) {
            if (map.containsKey(el)) {
                map.put(el, map.get(el) + 1);
            } else {
                map.put(el, 1);
            }
    
        }
    
        Set<Integer> keys = map.keySet();
        for (int key : keys) {
            if (map.get(key) > max) {
                max = map.get(key);
            }
    
        }
    
        return arr.size() - max;
    
    }
    
  • + 0 comments

    Javascript:

    function equalizeArray(arr) {
        const counts = {};
        let max = 0;
        for (let i = 0; i < arr.length; i++) {
            counts[arr[i]] = (counts[arr[i]] || 0) + 1;
            max = Math.max(counts[arr[i]], max);
        }
        return arr.length - max;
    }
    
  • + 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());  
        }