-
-
Save daboross/f65be8feb62e92c0e45e3649a06f6826 to your computer and use it in GitHub Desktop.
Fastest I know
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; | |
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