re: two loops

Yup! No shame in looking at other peoples code to get ideas. Not at all. One lesson to learn here is that solving an algorithm is just the first step in the process. The next step is to make it more efficient, and make it more readable. This code looks like a first pass. Give it another pass and see how clean you can make it.