Instantly share code, notes, and snippets.

@daboross /main.rs Secret
Created Dec 29, 2018

Embed
What would you like to do?
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