Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution

// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;
use std::hash::Hash;

/// Counter counts the number of times each value of type T has been seen.
struct Counter<T> {
    counts: HashMap<T, u64>,
}

impl<T: Eq + Hash> Counter<T> {
    /// Create a new Counter.
    fn new() -> Self {
        Counter { counts: HashMap::new() }
    }

    /// Count an occurrence of the given value.
    fn count(&mut self, item: T) {
        *self.counts.entry(item).or_default() += 1;
    }

    /// Return the number of times the given value has been seen.
    fn times_seen(&self, item: T) -> u64 {
        self.counts.get(&item).copied().unwrap_or_default()
    }
}

fn main() {
    let mut charctr = Counter::new();
    charctr.count(' ');
    charctr.count('a');
    charctr.count('¥');
    charctr.count('a');
    println!(
        "most common character: {:?}",
        charctr.counts.keys().max_by_key(|&c| charctr.times_seen(*c))
    );

    let mut intctr = Counter::new();
    intctr.count(13);
    intctr.count(14);
    intctr.count(16);
    intctr.count(14);
    intctr.count(14);
    intctr.count(11);

    for i in 10..20 {
        println!("saw {} values equal to {}", intctr.times_seen(i), i);
    }

    let mut strctr = Counter::new();
    strctr.count("apple");
    strctr.count("orange");
    strctr.count("apple");
    println!("got {} apples", strctr.times_seen("apple"));
}