Duplicate Character Counter in Rust

Published on 09 April 2023 (Updated: 09 April 2023)

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

Current Solution

use std::env::args;
use std::process::exit;
use std::collections::{HashMap, HashSet};

fn usage() -> ! {
    println!("Usage: please provide a string");

fn duplicate_character_counter(s: &str) -> HashMap<char, usize> {
    let mut counts: HashMap<char, usize> = HashMap::new();

    // Count number of occurances of each character
    for c in s.chars() {
        *counts.entry(c).or_insert(0) += 1;


fn show_duplicate_character_counts(
    s: &str, counts: &HashMap<char, usize>
) {
    // Show characters that have duplicates and keep track of
    // which duplicate characters are found
    let mut visited: HashSet<char> = HashSet::new();
    for c in s.chars() {
        let count: usize = *counts.get(&c).unwrap();
        if count > 1 && !visited.contains(&c) {
            println!("{c}: {count}");

    // Indicate if no duplicates found
    if visited.len() < 1 {
        println!("No duplicate characters");

fn main() {
    let mut args = args().skip(1);

    // Get 1st command-line argument. Error if empty
    let s: &str = &args
        .unwrap_or_else(|| usage());
    if s.len() < 1 {

    // Count duplicate characters and show results
    let counts: HashMap<char, usize> = duplicate_character_counter(&s);
    show_duplicate_character_counts(&s, &counts);

Duplicate Character Counter in Rust was written by:

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

Note: The solution shown above is the current solution in the Sample Programs repository as of May 08 2023 19:53:07. The solution was first committed on Apr 09 2023 11:02:14. As a result, documentation below may be outdated.

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.