Menu
Coddy logo textTech

Recap - Key-Value Storage

Part of the Fundamentals section of Coddy's GO journey — lesson 93 of 109.

challenge icon

Challenge

Easy

Let's practice using maps with a simple word counter!

Your task is to implement a program that counts how many times each word appears in a list of words.

Requirements:

  1. Complete the addWord() function that adds a word to the counter map
  2. Complete the getCount() function that returns the count for a specific word
  3. Complete the removeWord() function that removes a word from the counter

This is a simple recap of how to use maps as key-value storage.

Note: The output will display the word counts in alphabetical order for consistency.

Try it yourself

package main

import (
	"fmt"
	"sort"
)

// addWord adds a word to the counter map
func addWord(counter map[string]int, word string) {
	// Your code here
}

// getCount returns the count for a specific word
func getCount(counter map[string]int, word string) int {
	// Your code here
	return 0
}

// removeWord removes a word from the counter
// Returns true if the word was found and removed
func removeWord(counter map[string]int, word string) bool {
	// Your code here
	return false
}

// printWordCounts prints the word counts in alphabetical order
func printWordCounts(counter map[string]int) {
	// Get all keys (words) from the map
	words := make([]string, 0, len(counter))
	for word := range counter {
		words = append(words, word)
	}
	
	// Sort the words alphabetically
	sort.Strings(words)
	
	// Print each word and its count in sorted order
	for _, word := range words {
		fmt.Printf("%s: %d\n", word, counter[word])
	}
}

func main() {
	// Initialize the word counter map
	wordCounter := make(map[string]int)
	
	// Add some words
	words := []string{"apple", "banana", "apple", "orange", "banana", "apple"}
	for _, word := range words {
		addWord(wordCounter, word)
	}
	
	// Print the word counts
	fmt.Println("Word counts:")
	printWordCounts(wordCounter)
	
	// Get count for specific words
	fmt.Printf("\nCount for 'apple': %d\n", getCount(wordCounter, "apple"))
	fmt.Printf("Count for 'grape': %d\n", getCount(wordCounter, "grape"))
	
	// Remove a word
	removed := removeWord(wordCounter, "banana")
	fmt.Printf("\nRemoved 'banana': %v\n", removed)
	
	// Try to remove a non-existent word
	removed = removeWord(wordCounter, "grape")
	fmt.Printf("Removed 'grape': %v\n", removed)
	
	// Print the final word counts
	fmt.Println("\nFinal word counts:")
	printWordCounts(wordCounter)
}

All lessons in Fundamentals