We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Equalize the Array
  5. Discussions

Equalize the Array

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1716 Discussions, By:

recency

Please Login in order to post a comment

  • da_estrella
    10 hours ago+ 0 comments

    Lua

    function equalizeArray(arr)
        local max
        local elements = {}
        
        for _, value in ipairs(arr) do
            elements[value] = (elements[value] or 0) + 1
            max = math.max(max or 0, elements[value])
        end
        
        return #arr - max;
    end
    
    0|
    Permalink
  • villarroelfrank1
    2 days ago+ 0 comments

    Simple Swift Solution

    func equalizeArray(arr: [Int]) -> Int {
        var memberAmount = [Int: Int]()
        let distinct = Array(Set(arr))
        for i in 0..<distinct.count {
            let count = arr.filter { $0 == distinct[i] }.count
            memberAmount[distinct[i]] = count
        }
        return arr.count - (memberAmount.values.max() ?? 0)
    }
    
    0|
    Permalink
  • 20f246
    4 days ago+ 0 comments

    public static int equalizeArray(List arr) { int count[]= new int[1000]; int max=0; for(Integer i: arr) { count[i]++; if(count[i]>max) { max=count[i]; } } return arr.size()-max; }

    0|
    Permalink
  • hasin_ahbab
    5 days ago+ 0 comments

    c#

    public static int equalizeArray(List<int> arr)
        {
                Dictionary<int, int> memberAmount = new Dictionary<int, int>();
                List<int> distinct = arr.Distinct().ToList();
                for(int i = 0; i< distinct.Count(); i++)
                {
                    int count = arr.Count(p => p == distinct[i]);
                    memberAmount.Add(distinct[i], count);
                }
                return arr.Count() - memberAmount.Values.Max();
        }
    
    0|
    Permalink
  • orela231089
    6 days ago+ 0 comments

    **java script o(n) **

    function equalizeArray(arr) {
      if (arr.length === 1) return 0;
      const obj = {};
      for (let i = 0; i < arr.length; i++) {
        obj[arr[i]] = ++obj[arr[i]] || 1;
      }
      let max = 0;
      let sum = 0;
      for (const key in obj) {
        max = Math.max(max, obj[key]);
        sum = sum + obj[key];
      }
    
      return sum - max;
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy