Sort by

recency

|

1955 Discussions

|

  • + 0 comments

    Python3 using frequency count method

    Idea is to find the frequency of each elements and then to count the no. of delete operation needed we can just use below formula

    OperationNeeded = total number of elements - freq of that element

    def equalizeArray(arr):
        freq = {}
        for a in arr:
            freq[a] = freq.get(a, 0) + 1
        minimum = 1000
        total = len(arr)
        for k in freq.keys():
            deleteOperations = total - freq[k]
            minimum = min(minimum, deleteOperations)
        
        return minimum
    
  • + 0 comments

    Python3 Solution

    def equalizeArray(arr):
        
        freqNum = {}
        
        for num in arr:
            if num not in freqNum:
                freqNum[num] = 1
            else:
                freqNum[num] += 1
        
        mostFreqNum = max(freqNum, key=freqNum.get)
    
        return len(arr) - freqNum[mostFreqNum]
    
  • + 0 comments
    Map<Integer,Integer> map=new HashMap<>();
            for(int i=0;i<arr.size();i++){
                map.put(arr.get(i), Collections.frequency(arr, arr.get(i)));
                
            }
            int max=0;
            int count=0;
            for (Integer value : map.values()) {
                if(value>max){
                    max=value;
                }
                count=count+value;
            }
            return count-max;
    
  • + 0 comments

    Here is Equalize the array solution in python, java, c++, c and javascript programming - https://programmingoneonone.com/hackerrank-equalize-the-array-problem-solution.html

  • + 0 comments
    def equalizeArray(arr):
        # Write your code here
        dict1={}
        list1=[]
        for i in arr:
            dict1[i]=arr.count(i)
        for j in dict1.values():
            list1.append(j)
        list1.remove(max(list1))
        return sum(list1)``