Fastest I know
| use std::collections::HashMap; | |
| use std::io::{self, prelude::*}; | |
| struct Counter { | |
| items: HashMap<String, usize>, | |
| } | |
| impl Counter { | |
| fn new() -> Counter { | |
| return Counter { | |
| items: HashMap::with_capacity(1000), | |
| }; | |
| } | |
| fn add(&mut self, st: &str) { | |
| match self.items.get_mut(st) { | |
| Some(x) => { | |
| *x += 1; | |
| } | |
| None => { | |
| self.items.insert(st.to_owned(), 1); | |
| } | |
| } | |
| } | |
| fn render(&self) { | |
| for (key, val) in &self.items { | |
| println!("{}\t{}", val, key); | |
| } | |
| } | |
| } | |
| fn main() -> Result<(), io::Error> { | |
| let mut c = Counter::new(); | |
| let mut line = String::new(); | |
| let stdin = io::stdin(); | |
| let mut stdin_lock = stdin.lock(); | |
| while stdin_lock.read_line(&mut line)? != 0 { | |
| c.add(line.trim_end()); | |
| line.clear(); | |
| } | |
| c.render(); | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment