use a hashmap instead of an array - then you have constant-time lookups instead of iterating each time