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.
Equalize the Array
Equalize the Array
+ 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 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 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 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 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; }
Load more conversations
Sort 1716 Discussions, By:
Please Login in order to post a comment