And my favorite answer to questions like this is, "Can I just use grep (or shell commands in general)?"
Grep, uniq, wc, and a few others can be treated as pipeline data transformers to answer questions like this interview question. As long as you make some smart decisions about the order of operations, you can usually get performance on par with what you might write custom code for.