📖 Scenario: Imagine you are building a text editor feature that highlights the first character in a sentence that does not repeat. This helps users quickly spot unique characters.
🎯 Goal: Build a Go program that finds the first non-repeating character in a given string using a hash map (Go map).
📋 What You'll Learn
Create a string variable with a specific sentence
Create a map to count character frequencies
Use a loop to fill the map with counts
Find and print the first character that appears only once
💡 Why This Matters
🌍 Real World
Finding unique characters quickly is useful in text processing, spell checkers, and coding interview problems.
💼 Career
Understanding hash maps and string iteration is essential for software developers working with data parsing and optimization.
Progress0 / 4 steps
1
Create the input string
Create a string variable called input and set it to "swiss".
DSA Go
Hint
Use input := "swiss" to create the string.
2
Create a map to count characters
Create a map called charCount with key type rune and value type int to store character counts.
DSA Go
Hint
Use make(map[rune]int) to create the map.
3
Count each character in the string
Use a for loop with variable ch to iterate over input and increment charCount[ch] by 1 for each character.
DSA Go
Hint
Use for _, ch := range input to loop over characters.
4
Find and print the first non-repeating character
Use a for loop with variable ch to iterate over input. Inside the loop, check if charCount[ch] equals 1. If yes, print ch using fmt.Printf("%c\n", ch) and break the loop.
DSA Go
Hint
Print the character with fmt.Printf("%c\n", ch) and stop after the first one.
Practice
(1/5)
1. What is the main purpose of using a hash map in finding the first non-repeating character in a string?
easy
A. To count the frequency of each character efficiently
B. To sort the characters alphabetically
C. To reverse the string quickly
D. To store characters in a linked list
Solution
Step 1: Understand the role of hash map
A hash map stores each character as a key and counts how many times it appears in the string.
Step 2: Use frequency to find non-repeating character
By checking the counts, we can find the first character with count 1, which is the first non-repeating character.
Final Answer:
To count the frequency of each character efficiently -> Option A
Quick Check:
Hash map counts characters = frequency count [OK]
Hint: Hash maps count characters fast to find unique ones [OK]
Common Mistakes:
Thinking hash map sorts characters
Using hash map to reverse string
Confusing hash map with linked list
2. Which of the following Go code snippets correctly declares a hash map to store character counts for a string?
easy
A. counts := make(map[string]int)
B. counts := make([]int, len(s))
C. counts := make(map[int]string)
D. counts := make(map[rune]int)
Solution
Step 1: Identify correct map key type for characters
In Go, characters from strings are best represented as 'rune' type, so map[rune]int is correct for counting characters.
Step 2: Check other options
counts := make(map[string]int) uses string as key which is incorrect for single characters; counts := make(map[int]string) reverses key and value types; counts := make([]int, len(s)) is a slice, not a map.
Final Answer:
counts := make(map[rune]int) -> Option D
Quick Check:
Use map[rune]int for character counts in Go [OK]
Hint: Use map[rune]int to count characters in Go strings [OK]
Common Mistakes:
Using map[string]int for single characters
Confusing slices with maps
Using wrong key-value types in map
3. What will be the output of the following Go code snippet?
package main
import "fmt"
func main() {
s := "swiss"
counts := make(map[rune]int)
for _, ch := range s {
counts[ch]++
}
for _, ch := range s {
if counts[ch] == 1 {
fmt.Println(string(ch))
break
}
}
}
medium
A. w
B. s
C. i
D. No output
Solution
Step 1: Count character frequencies in "swiss"
Characters and counts: 's' = 3, 'w' = 1, 'i' = 1.
Step 2: Find first character with count 1 in order
Order in string: 's'(3), 'w'(1), 'i'(1), 's'(3), 's'(3). First with count 1 is 'w'.
Final Answer:
w -> Option A
Quick Check:
First unique char in "swiss" is 'w' [OK]
Hint: Check counts in string order; first count 1 is answer [OK]
Common Mistakes:
Picking 's' which repeats
Choosing 'i' ignoring order
Expecting no output
4. Identify the bug in this Go code that tries to find the first non-repeating character:
package main
import "fmt"
func main() {
s := "hello"
counts := make(map[rune]int)
for i := 0; i < len(s); i++ {
counts[rune(s[i])] = counts[rune(s[i])] + 1
}
for i := 0; i < len(s); i++ {
if counts[rune(s[i])] == 1 {
fmt.Println(s[i])
break
}
}
}
medium
A. Loop should use range instead of index
B. Map key should be string, not rune
C. Printing s[i] prints byte, not character
D. Counts map is not initialized
Solution
Step 1: Check how character is printed
Printing s[i] prints a byte as integer, not the character itself.
Step 2: Fix printing to convert byte to string
Use fmt.Println(string(s[i])) to print the character properly.
Final Answer:
Printing s[i] prints byte, not character -> Option C
Quick Check:
Print string(s[i]) to show character, not byte [OK]
Hint: Convert byte to string when printing characters [OK]
Common Mistakes:
Ignoring printing type mismatch
Changing map key type unnecessarily
Not initializing map (already done)
5. Given the string "aabbccdde", which code modification will correctly find the first non-repeating character using a hash map in Go, considering the string may contain Unicode characters?
hard
A. Use map[byte]int and iterate with 'for i := 0; i < len(s); i++'
B. Use map[rune]int and iterate with 'for _, ch := range s' to count frequencies
C. Use map[string]int and split string into substrings of length 2
D. Use map[int]int and convert characters to ASCII codes manually
Solution
Step 1: Choose correct map key type for Unicode
Using map[rune]int allows counting Unicode characters correctly, unlike byte which only handles ASCII.
Step 2: Use range loop to iterate runes
Using 'for _, ch := range s' iterates over runes, correctly handling multi-byte Unicode characters.
Final Answer:
Use map[rune]int and iterate with 'for _, ch := range s' to count frequencies -> Option B
Quick Check:
map[rune]int + range loop handles Unicode correctly [OK]
Hint: Use rune keys and range loop for Unicode-safe counting [OK]
Common Mistakes:
Using byte keys breaks Unicode characters
Splitting string incorrectly into substrings
Using int keys without clear mapping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.