Skip to content

Instantly share code, notes, and snippets.

@daboross

daboross/main.rs Secret

Created December 29, 2018 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daboross/f65be8feb62e92c0e45e3649a06f6826 to your computer and use it in GitHub Desktop.
Save daboross/f65be8feb62e92c0e45e3649a06f6826 to your computer and use it in GitHub Desktop.
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