-
-
Save daboross/3ce0706d636867611174e0e7484a2447 to your computer and use it in GitHub Desktop.
Faster but not fastest
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: 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