Finding duplicates in a stream of numbers
There might be other variations, but your answer should be pretty good.

There are two improvements to your algorithm, though. 


Instead of a hashmap that counts the number of times each integer appears, you can just use a hashset to record the appearance of each integer. 
While you are calling foo() in a loop, you will return immediately if you find the returned integer is in the hashset and if you are not required to finish the loop till the returned value of foo() is -1. Many built-in implementations return false when you insert an existing value into a hashset.