Menu
Coddy logo textTech

Unique Item Collector

Part of the Logic & Flow section of Coddy's GO journey — lesson 68 of 68.

challenge icon

Challenge

Easy

Create 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:

  1. Create a function called getUniqueItems that takes a slice of strings and returns a new slice containing only the unique strings
  2. Inside this function, use the map[string]struct{} idiom to track which items have been seen
  3. 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
  4. Parse the first input by splitting on commas to get the initial item list
  5. Parse the second input by splitting on commas to get the additional items
  6. Display the system header: "=== UNIQUE ITEM COLLECTOR ==="
  7. Display the original items: "Original items: [comma-separated list of all original items]"
  8. Display the additional items: "Additional items: [comma-separated list of all additional items]"
  9. Use your getUniqueItems function to get unique items from the original list
  10. Display the unique original items: "Unique original items: [comma-separated list of unique items from original list]"
  11. Combine both input slices into a single slice containing all items
  12. Use your getUniqueItems function to get unique items from the combined list
  13. Display the final unique items: "Final unique items: [comma-separated list of all unique items]"
  14. 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]"
  15. 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