Duplicate Character Counter in F#

Published on 08 April 2026 (Updated: 08 April 2026)

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

Current Solution

open System

let usage = "Usage: please provide a string"

module DuplicateCharacterCounter =
    let private getDuplicateCounts =
        Seq.countBy id
        >> Seq.filter (fun (_, count) -> count > 1)
        >> Seq.map (fun (c, count) -> sprintf "%c: %d" c count)
        >> Seq.toList

    let private formatOutput =
        function
        | [] -> "No duplicate characters"
        | items -> String.concat "\n" items

    let run input =
        input |> getDuplicateCounts |> formatOutput |> Ok

module Helpers =
    let (|Empty|NonEmpty|) (s: string) =
        match s.Trim() with
        | "" -> Empty
        | trimmed -> NonEmpty trimmed

    let parseArgs argv =
        match argv with
        | [| Empty |] -> Error usage
        | [| NonEmpty input |] -> Ok input
        | _ -> Error usage

    let handleResults =
        function
        | Ok result ->
            printfn "%s" result
            0
        | Error msg ->
            eprintfn "%s" msg
            1

[<EntryPoint>]
let main argv =
    argv
    |> Helpers.parseArgs
    |> Result.bind DuplicateCharacterCounter.run
    |> Helpers.handleResults

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