Iterator-based solution for 2.