Menu
Coddy logo textTech

Recap - Unique Usernames

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

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Build a username registration system that prevents duplicate usernames using the Go set idiom. This challenge demonstrates how to create a robust user management system that ensures username uniqueness across all registrations.

You will receive two inputs:

  • A string containing existing usernames separated by commas (e.g., "alice123,bob_dev,charlie2024,diana_codes")
  • A string containing new username registration attempts separated by commas (e.g., "eve_user,alice123,frank_dev,bob_dev,grace2024,alice123")

Your task is to:

  1. Create a set using the Go idiom map[string]struct{} to store registered usernames
  2. Parse the first input string by splitting it on commas to get existing usernames
  3. Add each existing username to your set using the empty struct literal {} as the value
  4. Parse the second input string by splitting it on commas to get new registration attempts
  5. For each registration attempt, check if the username already exists in the set using the comma ok idiom
  6. Display the registration process for each attempt:
    • If the username is available: "Registered: [username]" and add it to the set
    • If the username already exists: "Username taken: [username]"
  7. After processing all registration attempts, display a comprehensive summary:
    • "Registration Summary:"
    • "Initial usernames: [initial_count]"
    • "Registration attempts: [attempts_count]"
    • "Successful registrations: [successful_count]"
    • "Rejected (duplicate): [rejected_count]"
    • "Total registered usernames: [total_count]"
  8. Finally, list all registered usernames in the system:
    • Header: "All registered usernames:"
    • Each username on a separate line: "@[username]"

Use the strings package to split the input strings on commas. This challenge demonstrates how the Go set idiom provides an efficient solution for preventing duplicates in user registration systems, ensuring data integrity while providing fast lookup performance for username validation.

Try it yourself

package main

import (
	"fmt"
	"sort"
	"strings"
)

func main() {
	// Read input
	var existingUsernames string
	var newRegistrations string
	fmt.Scanln(&existingUsernames)
	fmt.Scanln(&newRegistrations)
	
	// Parse input strings
	existing := strings.Split(existingUsernames, ",")
	attempts := strings.Split(newRegistrations, ",")
	
	// Create set using Go idiom
	userSet := make(map[string]struct{})
	
	// TODO: Write your code below
	// 1. Add existing usernames to the set
	// 2. Process each registration attempt
	// 3. Display registration results for each attempt
	// 4. Calculate and display the summary
	// 5. List all registered usernames (remember to sort for consistent output)
	
}

All lessons in Logic & Flow