Federico Pintaluba
I'm a 28 years old developer, I have been coding for more than a decade, but I have focused in Android Development for about 7 years, and in Flutter for the past 2 years. Here you will find all my projects, ideas, thoughts. Basically everything I made with <3.

HackerRank: Equalize the Array


Karl has an array of integers. He wants to reduce the array until all remaining elements are equal. Determine the minimum number of elements to delete to reach his goal.

For example, if his array is arr = [1, 2, 2, 3], we see that he can delete the 2 elements 1 and 3 leaving arr = [2, 2]. He could also delete both twos and either the 1 or the 3, but that would take 3 deletions. The minimum number of deletions is 2.

Function Description

Complete the equalizeArray function in the editor below. It must return an integer that denotes the minimum number of deletions required.

equalizeArray has the following parameter(s):

  • arr: an array of integers

Input Format

The first line contains an integer n, the number of elements in arr.
The next line contains n space-separated integers arr[i].


Output Format

Print a single integer that denotes the minimum number of elements Karl must delete for all elements in the array to be equal.

Sample Input

3 3 2 1 3

Sample Output



Array arr = [3, 3, 2, 1, 3]. If we delete arr[2] = 2 and arr[3] = 1, all of the elements in the resulting array, A’ = [3, 3, 3], will be equal. Deleting these 2 elements is minimal. Our only other options would be to delete 4 elements to get an array of either [1] or [2].


We know that our array will never be empty, based on Constraints, so we don’t need to make checks for that at all.

Let’s start by grouping our values:

arr.groupBy { it }

This will give us a map with all repeated values as a List for each Int (key), now we want to count how many groups of numbers we have, we just need to look for the count() function of the Map, but, Kotlin offers us a quicker way using Grouping and groupingBy, this allow us to group and apply right away, check this out:

arr.groupingBy { it }.eachCount()

By doing this, we have right away how many groups of repeated Ints we have, now what?

If we find out which group has the most repeated Ints, then removing them for the list will give us the list we need to remove from the original one, but we just want the length, so let’s do something like this:

arr.groupingBy { it }.eachCount().maxBy { it.value }!!.value

Explaining this further, we first group our list into a Map, now we have <key, value> where key is the Int and value is the list of the repeated in the original list.

Since we don’t need the whole list at all, just the amount of them for each key, by calling eachCount() we convert this Grouping into another Map, but this time of type <Int, Int>, for each key, we know how many of them are repeated.

Now we find the Pair that has the maximum value, we now that substracting this number from the original list size, we get the minimum number of deletions required. So we must return the value of the max Pair:

return arr.groupingBy { it }.eachCount().maxBy { it.value }!!.value

And that’s it, we did it in one line of code, and we can write the function to have expression body syntax like this:

fun equalizeArray(arr: Array<Int>): Int = arr.size - arr.groupingBy{it}.eachCount().maxBy{it.value}!!.value

Let’s run this in HackerRank and see if we pass all the test cases!


Hope this helped you understand a fair solution made in Kotlin for this problem, if you have any suggestion or a better way of doing this in the same language, don’t hesistate on commenting below!


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *