iterative, use hashset to determine when to print