Sling Academy
Home/Golang/Using Maps for Counting Occurrences in Go

Using Maps for Counting Occurrences in Go

Last updated: November 24, 2024

In Go (also known as Golang), using maps to count occurrences of items in a dataset is a common approach due to the efficient key-value structure of maps. This technique can be employed to count occurrences of strings, numbers, or any other comparable data type.

Basic Example

Let's begin with a basic example: counting occurrences of words in a slice of strings.

package main

import "fmt"

func main() {
    words := []string{"apple", "banana", "apple", "orange", "banana", "apple"}
    wordCount := make(map[string]int)

    for _, word := range words {
        wordCount[word]++
    }

    fmt.Println(wordCount) // Output: map[apple:3 banana:2 orange:1]
}

In the code above, we initialize a map called wordCount whose keys are strings and values are integers. We iterate over each word in the words slice and increment the count for each word in the map.

Intermediate Example

Next, let's consider counting occurrences with an integer slice. Here’s how you can count numbers:

package main

import "fmt"

func main() {
    numbers := []int{1, 2, 3, 1, 2, 1, 3, 4, 3, 3}
    numCount := make(map[int]int)

    for _, num := range numbers {
        numCount[num]++
    }

    fmt.Println(numCount) // Output: map[1:3 2:2 3:4 4:1]
}

Similar to strings, we use integers as keys with corresponding count values.

Advanced Example

For an advanced example, suppose you have a struct and you want to count occurrences based on one of the struct’s fields. Here’s how you do it in Go:

package main

import "fmt"

// Define a struct type
type Person struct {
    name string
    age  int
}

func main() {
    people := []Person{
        {name: "John", age: 30},
        {name: "Jane", age: 30},
        {name: "John", age: 40},
        {name: "Alice", age: 30},
    }

    ageCount := make(map[int]int)

    for _, person := range people {
        ageCount[person.age]++
    }

    fmt.Println(ageCount) // Output: map[30:3 40:1]
}

The challenge here is to correctly count occurrences based on a specific field of the struct. By iterating over the struct slice, you effectively use maps to keep track of occurrences of the age attribute in the Person struct.

These examples illustrate how flexible and powerful maps are in Go when counting occurrences, making it easier to process and analyze collections of data efficiently.

Next Article: Practical Examples of Maps in Web Development with Go

Previous Article: Maps and Concurrency in Go: Safe Practices and Pitfalls

Series: Working with Maps in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant