made code safer to account for integers that appear more than twice