Hash Tables
Group by Key
Build buckets keyed by a shared field, preserving the first-seen key order.
Algorithm
Canonical pairs (a,1), (b,2), (a,3), (c,4), (b,5) print
{a: [1, 3], b: [2, 5], c: [4]}.
The replay uses the same input in every language, so this Go DSA
implementation can be compared directly with the rest of the DSA track.
bucket map
Each key owns a list. A new key creates a bucket; a repeated key appends to the existing bucket.
Basic Implementation
basic.go
Replay: real traced execution (multi-file project)
package main
import (
"fmt"
"strings"
)
type pair struct {
key string
value int
}
func main() {
pairs := []pair{{"a", 1}, {"b", 2}, {"a", 3}, {"c", 4}, {"b", 5}}
groups := map[string][]int{}
order := []string{}
for _, item := range pairs {
if _, ok := groups[item.key]; !ok {
order = append(order, item.key)
}
groups[item.key] = append(groups[item.key], item.value)
}
parts := []string{}
for _, key := range order {
values := groups[key]
renderedValues := []string{}
for _, value := range values {
renderedValues = append(renderedValues, fmt.Sprintf("%d", value))
}
parts = append(parts, fmt.Sprintf("%s: [%s]", key, strings.Join(renderedValues, ", ")))
}
fmt.Println("{" + strings.Join(parts, ", ") + "}")
}
pairs ← [(a, 1), (b, 2), (a, 3), (c, 4), (b, 5)]
1package mainvalues this step[(a, 1), (b, 2), (a, 3), (c, 4), (b, 5)]pairsgroups ← {}
14pairs := []pair{{"a", 1}, {"b", 2}, {"a", 3}, {"c", 4}, {"b", 5}}15groups := map[string][]int{}16order := []string{}values this step{}groupsgroups ← {a: [1]}
14pairs := []pair{{"a", 1}, {"b", 2}, {"a", 3}, {"c", 4}, {"b", 5}}15groups := map[string][]int{}16order := []string{}values this step{} → {a: [1]}groupsakey1valuegroups ← {a: [1], b: [2]}
14pairs := []pair{{"a", 1}, {"b", 2}, {"a", 3}, {"c", 4}, {"b", 5}}15groups := map[string][]int{}16order := []string{}values this step{a: [1]} → {a: [1], b: [2]}groupsbkey2valuegroups ← {a: [1, 3], b: [2]}
14pairs := []pair{{"a", 1}, {"b", 2}, {"a", 3}, {"c", 4}, {"b", 5}}15groups := map[string][]int{}16order := []string{}values this step{a: [1], b: [2]} → {a: [1, 3], b: [2]}groupsakey3valuegroups ← {a: [1, 3], b: [2], c: [4]}
14pairs := []pair{{"a", 1}, {"b", 2}, {"a", 3}, {"c", 4}, {"b", 5}}15groups := map[string][]int{}16order := []string{}values this step{a: [1, 3], b: [2]} → {a: [1, 3], b: [2], c: [4]}groupsckey4valuegroups ← {a: [1, 3], b: [2, 5], c: [4]}
14pairs := []pair{{"a", 1}, {"b", 2}, {"a", 3}, {"c", 4}, {"b", 5}}15groups := map[string][]int{}16order := []string{}values this step{a: [1, 3], b: [2], c: [4]} → {a: [1, 3], b: [2, 5], c: [4]}groupsbkey5valuestdout ← {a: [1, 3], b: [2, 5], c: [4]}
27for _, value := range values {28 renderedValues = append(renderedValues, fmt.Sprintf("%d", value))29}values this step{a: [1, 3], b: [2, 5], c: [4]}stdout{a: [1, 3], b: [2, 5], c: [4]}groupsbucket 1 after collision ← c -> a, degradation risk ← long chains can degrade lookup toward O(n)
27for _, value := range values {28 renderedValues = append(renderedValues, fmt.Sprintf("%d", value))29}values this stepa → c -> abucket 1 after collisionlong chains can degrade lookup toward O(n)degradation riskresize or rehash when load factor growsmitigationcnew key
Complexity
- Time: O(n) average
- Space: O(k + n) for buckets and values
Implementation notes
- Keep output formatting deterministic. Do not rely on unordered hash-map printing when the lesson needs cross-language comparison.
- The trace highlights the hash table state after each write and includes a collision contrast where one bucket chain grows, showing why long chains can degrade lookup and why real tables resize or rehash.