Attempt at problem 4 with HashMap.