Skip to content

Instantly share code, notes, and snippets.

@daboross

daboross/main.rs Secret

Created December 29, 2018 21:31
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 daboross/3ce0706d636867611174e0e7484a2447 to your computer and use it in GitHub Desktop.
Save daboross/3ce0706d636867611174e0e7484a2447 to your computer and use it in GitHub Desktop.
Faster but not fastest
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: String) {
let x = self.items.entry(st).or_insert(0);
*x += 1;
}
fn render(&self) {
for (key, val) in &self.items {
println!("{}\t{}", val, key);
}
}
}
fn main() -> Result<(), io::Error> {
let mut c = Counter::new();
for line in io::stdin().lock().lines() {
c.add(line?);
}
c.render();
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment