Solution for problem 14 (easy) using Java collections