Skip to content

Instantly share code, notes, and snippets.

@Shaddy
Created August 24, 2017 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shaddy/9ac80f791d273aff9578888cc09b8186 to your computer and use it in GitHub Desktop.
Save Shaddy/9ac80f791d273aff9578888cc09b8186 to your computer and use it in GitHub Desktop.
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