Unique Item Collector
Part of the Logic & Flow section of Coddy's GO journey — lesson 68 of 68.
Challenge
EasyCreate a duplicate removal system that uses Go's idiomatic set pattern to filter unique items from collections of strings. This challenge will test your ability to use the map[string]struct{} idiom to track seen items and build collections containing only unique elements.
You will receive two inputs:
- A string containing items separated by commas (e.g.,
"apple,banana,apple,orange,banana,grape,apple") - A string containing additional items to merge, also separated by commas (e.g.,
"kiwi,apple,mango,banana,kiwi")
Your task is to:
- Create a function called
getUniqueItemsthat takes a slice of strings and returns a new slice containing only the unique strings - Inside this function, use the
map[string]struct{}idiom to track which items have been seen - Iterate through the input slice and for each item:
- Check if the item exists in your set using the comma ok idiom
- If the item hasn't been seen before, add it to both the set and the result slice
- Parse the first input by splitting on commas to get the initial item list
- Parse the second input by splitting on commas to get the additional items
- Display the system header:
"=== UNIQUE ITEM COLLECTOR ===" - Display the original items:
"Original items: [comma-separated list of all original items]" - Display the additional items:
"Additional items: [comma-separated list of all additional items]" - Use your
getUniqueItemsfunction to get unique items from the original list - Display the unique original items:
"Unique original items: [comma-separated list of unique items from original list]" - Combine both input slices into a single slice containing all items
- Use your
getUniqueItemsfunction to get unique items from the combined list - Display the final unique items:
"Final unique items: [comma-separated list of all unique items]" - Display collection statistics:
"=== COLLECTION STATISTICS ===""Total original items: [count of original items]""Total additional items: [count of additional items]""Total combined items: [count of all items combined]""Unique items found: [count of unique items]""Duplicates removed: [total combined items minus unique items]"
- Display the completion message:
"Unique item collection completed successfully"
Use the strings package to split input strings and the fmt package for output. When joining items for display, use strings.Join with a comma separator. This challenge demonstrates how Go's set idiom efficiently solves the common problem of removing duplicates from collections, a pattern you'll use frequently in data processing applications.
Try it yourself
package main
import (
"fmt"
"strings"
)
func main() {
// Read input
var input1 string
var input2 string
fmt.Scanln(&input1)
fmt.Scanln(&input2)
// Parse input strings into slices
originalItems := strings.Split(input1, ",")
additionalItems := strings.Split(input2, ",")
// TODO: Write your code here
// 1. Create the getUniqueItems function that uses map[string]struct{} idiom
// 2. Process the original items to get unique items
// 3. Combine both slices and get unique items from combined list
// 4. Calculate statistics
// Display system header
fmt.Println("=== UNIQUE ITEM COLLECTOR ===")
// Display original and additional items
fmt.Printf("Original items: %s\n", strings.Join(originalItems, ","))
fmt.Printf("Additional items: %s\n", strings.Join(additionalItems, ","))
// TODO: Display unique original items, final unique items, and statistics
// Use fmt.Printf and strings.Join for output formatting
}All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors