David in Canada

 

Tech, life, and everything in between.

Day 6 — Using Maps for Frequency Analysis in Go

Updated at # Tech # Go

Overview

Today I focused on Go’s map type and applied it to a classic problem: frequency counting.

Instead of just learning syntax, I implemented a small CLI tool that processes a list of integers and extracts useful insights from it.

This is a key step toward writing interview-ready code, since maps and frequency patterns appear frequently in real-world problems.


What I Built

A Frequency Analyzer CLI that:


Key Concepts

1. Maps in Go

m := make(map[int]int)

Maps store key-value pairs and are used for fast lookups and counting.


2. Frequency Counting Pattern

for _, num := range nums {
    freq[num]++
}

This is one of the most important patterns in programming and is widely used in interviews.


3. Iteration over Maps

for k, v := range freq {
    fmt.Println(k, v)
}

Functions Implemented

Build Frequency Map

func buildFrequency(nums []int) map[int]int

Find Most Frequent Element

func mostFrequent(freq map[int]int) (int, int)

Count Distinct Values

func countUnique(freq map[int]int) int

Find Single-Occurrence Values

func findSingles(freq map[int]int) []int

Key Issue Encountered

Map Iteration Order

I initially assumed that map iteration had a consistent order, but Go maps are unordered.

This means:

Resolution

I updated the logic to either:


Key Takeaways


Example Output

How many numbers? 6
Next number: 1
Next number: 2
Next number: 2
Next number: 3
Next number: 3
Next number: 3

Numbers: [1 2 2 3 3 3]

Frequency:
1 -> 1
2 -> 2
3 -> 3

Most frequent: 3 (count: 3)
Unique count: 3
Singles: [1]

Reflection

This exercise reinforced how powerful maps are for solving real problems.

It also highlighted an important concept: understanding behavior (like map ordering) is just as important as writing correct syntax.


Next Steps