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 Go. Let's get started!

Basic Aggregation Using Built-in Functions

Let's say we have a slice of integers denoting the ages of a group of people. We will demonstrate several basic aggregation methods using Go's slice manipulation techniques:

package main

import (
    "fmt"
    "slices"
)

func main() {
    ages := []int{21, 23, 20, 25, 22, 27, 24, 22, 25, 22, 23, 22}

    // Number of people
    numPeople := len(ages)
    fmt.Println("Number of people:", numPeople)

    // Youngest age
    youngestAge := slices.Min(ages)
    fmt.Println("Youngest age:", youngestAge)

    // Oldest age
    oldestAge := slices.Max(ages)
    fmt.Println("Oldest age:", oldestAge)

    // Average age
    totalAges := 0
    for _, age := range ages {
        totalAges += age
    }
    averageAge := float64(totalAges) / float64(numPeople)
    fmt.Println("Average age:", averageAge)

    // Age range
    ageRange := oldestAge - youngestAge
    fmt.Println("Age range:", ageRange)
}

Here's a brief overview of the above code snippet:

  1. Number of people: Uses len(ages) to get the number of elements in the slice.
  2. Youngest age: Uses slices.Min to find the smallest element in the slice.
  3. Oldest age: Uses slices.Max to find the largest element in the slice. Note that Min and Max functions are available starting from Go 1.21. Previously, finding the minimum and maximum required iterating with for loops, a common pattern in Go.
  4. Average age: Calculates the average age by dividing the total age by the number of people.
  5. Age range: Computes the range of ages by subtracting the youngest age from the oldest age.

These techniques provide essential aggregation operations and are widely used with data streams in Go.

Advanced Aggregation Using For Loops

For deeper analysis, such as calculating the mode or most frequent age, we can use Go's for loops, a common pattern in the language.

For example, let's see how we can find the mode or most frequent age:

package main

import (
    "fmt"
)

func main() {
    ages := []int{21, 23, 20, 25, 22, 27, 24, 22, 25, 22, 23, 22}

    // Initialize a map to store the frequency of each age
    frequencies := make(map[int]int)

    // Use a for loop to populate frequencies
    for _, age := range ages {
        frequencies[age]++
    }

    // Find the age with max frequency
    maxFreq := 0
    modeAge := -1
    for age, count := range frequencies {
        if count > maxFreq {
            maxFreq = count
            modeAge = age
        }
    }
    
    fmt.Println("Max frequency:", maxFreq)  // Max frequency: 4
    fmt.Println("Mode age:", modeAge)       // Mode age: 22
}

In this code, a map is used to keep track of the frequency of each age, and a for loop is used to populate it and to determine the most frequent age.

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