Introduction to Data Aggregation Methods

Welcome to today's lesson! Our topic for the day is data aggregation, a crucial aspect of data analysis. Like summarizing a massive book into key points, data aggregation summarizes large amounts of data into important highlights.

By the end of today, you'll be equipped with several aggregation methods to summarize data streams in Kotlin. Let's get started!

Basic Aggregation using Built-in Functions

Let's say we have a list of numbers denoting the ages of a group of people:

val ages = listOf(21, 23, 20, 25, 22, 27, 24, 22, 25, 22, 23, 22)

Common questions we might ask: How many people are in the group? What's their total age? Who's the youngest and the oldest? Kotlin's handy built-in functions size, sum, minOrNull, and maxOrNull have our answers:

val numPeople = ages.size     // Number of people (12)
val totalAges = ages.sum()    // Total age (276)
val youngestAge = ages.minOrNull() ?: 0 // Youngest age (20)
val oldestAge = ages.maxOrNull() ?: 0   // Oldest age (27)

// Use sum() and size to find the average age
val averageAge = ages.sum() / ages.size.toDouble() // Result: 23.0

// Use maxOrNull() and minOrNull() to find the range of ages
val ageRange = (ages.maxOrNull() ?: 0) - (ages.minOrNull() ?: 0) // Result: 7
  • size: This function returns the number of elements in the list, helping us determine how many people are in the group.
  • sum(): It calculates the total of all elements in the list, representing the combined age of all individuals.
  • minOrNull(): This function finds the smallest element in the list, which gives us the youngest age. The ?: 0 is used as a null safety check, providing a default value if the list is empty.
  • maxOrNull(): Similar to minOrNull(), this function finds the largest element, identifying the oldest age. The ?: 0 acts as a safeguard for an empty list.
  • toDouble(): This function converts the Int value to Double, ensuring precision when calculating the average age.
  • ageRange: This calculation determines the difference between the oldest and youngest ages, indicating the age span within the group.

These functions provide essential aggregation operations and are widely used with data streams.

Advanced Aggregation using For Loops

For deeper analysis, such as calculating the average age or range of ages, we resort to for loops.

For example, using for loops, we can also find the mode or most frequent age:

val ages = listOf(21, 23, 20, 25, 22, 27, 24, 22, 25, 22, 23, 22)

// Initialize a map to store the frequency of each age
val frequencies = mutableMapOf<Int, Int>()

// Use a for loop to populate frequencies
for (age in ages) {
    frequencies[age] = frequencies.getOrDefault(age, 0) + 1
}

// Find the age with a max frequency
var maxFreq = 0
var modeAge = -1
for ((age, freq) in frequencies) {
    if (freq > maxFreq) {
        maxFreq = freq
        modeAge = age
    }
}
println("Max frequency: $maxFreq") // Max frequency: 4
println("Mode age: $modeAge") // Mode age: 22
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal