feat(rust): add counting duplicates

This commit is contained in:
Dmitriy Pleshevskiy 2021-10-29 10:17:58 +03:00
parent 65014a1f22
commit 2adc74168d

View file

@ -0,0 +1,32 @@
use std::collections::HashMap;
fn count_duplicates(text: &str) -> u32 {
text.to_lowercase()
.chars()
.fold(HashMap::<char, u32>::new(), |mut hm, c| {
hm.insert(c, hm.get(&c).copied().unwrap_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);
}
}