# Min Deletion Unique Frequency

Minimum Deletions to Make Character Frequencies Unique

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good.

package questions

import _utils.UseCommentAsDocumentation
import utils.shouldBe

/**
* Minimum Deletions to Make Character Frequencies Unique
*
* A string s is called good if there are no two different characters in s that have the same frequency.
* Given a string s, return the minimum number of characters you need to delete to make s good.
*
* [Source](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/) – [Solution](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/1107954/Java-Simple-Solution)
*/
@UseCommentAsDocumentation
private fun minDeletions(s: String): Int {
val countArray = IntArray(26) { 0 } // Maintain array of counts
s.forEach {
countArray[it - 'a']++
}
val alreadyTaken = mutableSetOf<Int>() // the counts that has already been seen
var deletion = 0
for (i in 0..countArray.lastIndex) {
var counts = countArray[i]
while (counts > 0 && alreadyTaken.contains(counts)) {
countArray[i]-- // Count already exists so mark it for deletion
counts = countArray[i]
deletion++
}
}
return deletion
}

fun main() {