If you have permission to use that borrowed code, I'd suggest asking the author. Though here's an idea, use hash tables. Since you've borrowed the code, I'm not going to explain this, you'll have to figure it out on your own, sorry, that's just my view on "borrowed" code.