Duplicate Character Counter in Powershell

Published on 20 May 2025 (Updated: 20 May 2025)

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

Current Solution

function Show-Usage() {
    Write-Host "Usage: please provide a string"
    Exit 1
}

function Get-DuplicateCharacterCounter([string]$Str) {
    $Counts = @{}
    $Str.ToCharArray() | ForEach-Object { $Counts[$_] += 1 }
    $Counts
}

function Show-DuplicateCharacterCounter([string]$Str, [object]$Counts) {
    $Dupes = $false
    $Str.ToCharArray() | Select-Object -Unique | ForEach-Object {
        if ($Counts[$_] -gt 1) {
            Write-Host "${_}: $($Counts[$_])"
            $Dupes = $true
        }
    }

    if (-not $Dupes) {
        Write-Host "No duplicate characters"
    }
}

if ($args.Length -lt 1 -or -not $args[0]) {
    Show-Usage
}

$Str = $args[0]
$Counts = Get-DuplicateCharacterCounter $Str
Show-DuplicateCharacterCounter $Str $Counts

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