Changed from list to hashmap for faster lookup for large n