Sort by

recency

|

1946 Discussions

|

  • + 0 comments
        Comparator<Long> longComparator = Long::compare;
        Map<Integer, Long> valueMap = arr.stream().collect(Collectors.groupingBy(s -> s, Collectors.counting()));
        List<Long> values = valueMap.values().stream().sorted(longComparator.reversed()).collect(Collectors.toList());
        long mostrepeated = values.get(0);
        long totaloccurences= values.stream().mapToLong(s->s).sum();
        return (int)(totaloccurences - mostrepeated);
    
  • + 1 comment

    Jaav 8 solution

        int maxRepeat = 0;
    
            for(int i = 0; i < arr.size(); i++){
                int num = arr.get(i);
                int RepeatNum = 0;
    
                for(int j = 0; j < arr.size(); j++){
                    if(arr.get(j) == num){
                        RepeatNum++;
                    }
                }
                if(maxRepeat < RepeatNum){
                    maxRepeat = RepeatNum;
                }
            }
            return arr.size() - maxRepeat;
    
  • + 0 comments

    Python solution

    def equalizeArray(arr):
        arr_dict = Counter(arr)
        max_num_arr = max(arr_dict.values())
        return len(arr)-max_num_arr
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-equalize-the-array-problem-solution.html

  • + 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;
    }