Looping like this is often a code smell indicating that the wrong data structure is being used.