Created
August 24, 2017 22:27
-
-
Save Shaddy/9ac80f791d273aff9578888cc09b8186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::{HashMap, HashSet}; | |
pub fn word_count(input: &str) -> HashMap<String, u32> { | |
let mut map: HashMap<String, u32> = HashMap::new(); | |
let words: Vec<&str> = input.split_whitespace().collect(); | |
let set: HashSet<&str> = words.clone().into_iter().collect::<HashSet<&str>>(); | |
for word in set { | |
let count = words.iter().filter(|&w| *w == word).count() as u32; | |
map.insert(word.to_string(), count); | |
} | |
map | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment