feat(rust): add counting duplicates
This commit is contained in:
parent
65014a1f22
commit
2adc74168d
1 changed files with 32 additions and 0 deletions
32
rust/tests/2_counting_duplicates.rs
Normal file
32
rust/tests/2_counting_duplicates.rs
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue