0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Find Frequency of Elements

Use Kotlin's groupingBy with eachCount() to find frequency of elements like this: val freq = list.groupingBy { it }.eachCount().
📋

Examples

Input[1, 2, 2, 3, 3, 3]
Output{1=1, 2=2, 3=3}
Input["apple", "banana", "apple", "orange", "banana", "banana"]
Output{apple=2, banana=3, orange=1}
Input[]
Output{}
🧠

How to Think About It

To find how many times each element appears, think of counting each item as you see it. You can use a map to keep track of counts, adding 1 each time you find the same element again.
📐

Algorithm

1
Get the list of elements.
2
Create an empty map to store element counts.
3
For each element in the list, increase its count in the map by 1.
4
Return the map with elements as keys and their counts as values.
💻

Code

kotlin
fun main() {
    val list = listOf(1, 2, 2, 3, 3, 3)
    val frequency = list.groupingBy { it }.eachCount()
    println(frequency)
}
Output
{1=1, 2=2, 3=3}
🔍

Dry Run

Let's trace the list [1, 2, 2, 3, 3, 3] through the code

1

Start with list

list = [1, 2, 2, 3, 3, 3]

2

Group elements and count

groupingBy { it } creates groups by element value

3

Count each group

eachCount() counts how many times each element appears

4

Result map

frequency = {1=1, 2=2, 3=3}

ElementCount
11
22
33
💡

Why This Works

Step 1: Grouping elements

The groupingBy function groups elements by their value so we can count them easily.

Step 2: Counting occurrences

The eachCount() function counts how many elements are in each group, giving the frequency.

Step 3: Result as map

The result is a map where keys are elements and values are their counts.

🔄

Alternative Approaches

Manual counting with mutable map
kotlin
fun main() {
    val list = listOf(1, 2, 2, 3, 3, 3)
    val freqMap = mutableMapOf<Int, Int>()
    for (item in list) {
        freqMap[item] = freqMap.getOrDefault(item, 0) + 1
    }
    println(freqMap)
}
This method is more verbose but shows how counting works step-by-step.
Using fold
kotlin
fun main() {
    val list = listOf(1, 2, 2, 3, 3, 3)
    val freq = list.fold(mutableMapOf<Int, Int>()) { acc, i ->
        acc[i] = acc.getOrDefault(i, 0) + 1
        acc
    }
    println(freq)
}
Using fold is functional style but slightly more complex to read.

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

Time Complexity

The program loops through the list once, so time grows linearly with the number of elements.

Space Complexity

A map stores counts for each unique element, so space depends on how many different elements exist.

Which Approach is Fastest?

Using groupingBy with eachCount() is concise and optimized internally, usually faster and clearer than manual counting.

ApproachTimeSpaceBest For
groupingBy + eachCountO(n)O(n)Simple and efficient frequency count
Manual mutableMap countingO(n)O(n)Learning how counting works step-by-step
fold with mutableMapO(n)O(n)Functional style but less readable
💡
Use groupingBy { it }.eachCount() for a clean and efficient frequency count in Kotlin.
⚠️
Beginners often forget to initialize counts or overwrite counts instead of incrementing them.