Duplicate Character Counter in Go

Published on 08 October 2024 (Updated: 08 October 2024)

Welcome to the Duplicate Character Counter in Go page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

package main

import (
	"fmt"
	"os"
)

func errorMessage() {
	fmt.Println("Usage: please provide a string")
}

func main() {
	if len(os.Args) <= 1 {
		errorMessage()
		return
	}

	inputStr := os.Args[1]

	if len(inputStr) == 0 {
		errorMessage()
		return
	}
	charCount := make(map[rune]int)
	for _, c := range inputStr {
		charCount[c]++
	}
	duplicateFound := false
	for _, c := range inputStr {
		if count, exists := charCount[c]; exists && count > 1 {
			if !duplicateFound {
				duplicateFound = true
			}
			fmt.Printf("%c: %d\n", c, count)
			charCount[c] = 0
		}
	}

	if !duplicateFound {
		fmt.Println("No duplicate characters")
	}
}

Duplicate Character Counter in Go was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.