Duplicate Character Counter in Kotlin

Published on 07 October 2025 (Updated: 07 October 2025)

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

Current Solution

import kotlin.system.exitProcess

fun main(args: Array<String>) {
    val phrase: String = errorChecking(args)
    val counts = duplicateCharacterCount(phrase)
    outputMap(counts)
}

fun usageError() {
    println("Usage: please provide a string")
}

fun errorChecking(args: Array<String>): String {
    if (args.size == 0 || args[0] == "") {
       usageError()
       exitProcess(1)
    }
    return args[0]
}

fun duplicateCharacterCount(phrase: String): Map<Char, Int> {
    val counts: Map<Char, Int> = phrase.groupingBy { it }.eachCount()
    return counts.filter { it.value > 1 }
}

fun outputMap(counts: Map<Char, Int>) {
    if (counts.size > 0) {
        for (pair in counts) {
            println("${pair.key}: ${pair.value}")
        }
    } else {
        println("No duplicate characters")
    }
}

Duplicate Character Counter in Kotlin 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.