mykatas/rust/tests/2_counting_duplicates.rs

32 lines
662 B
Rust

use std::collections::HashMap;
fn count_duplicates(text: &str) -> u32 {
text.to_lowercase()
.chars()
.fold(HashMap::<char, u32>::new(), |mut hm, c| {
(*hm.entry(c).or_default()) += 1;
hm
})
.values()
.filter(|&&count| count > 1)
.count() as u32
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_abcde() {
assert_eq!(count_duplicates("abcde"), 0);
}
#[test]
fn test_abcdea() {
assert_eq!(count_duplicates("abcdea"), 1);
}
#[test]
fn test_indivisibility() {
assert_eq!(count_duplicates("indivisibility"), 1);
}
}