You can modify the array in-place while iterating by keeping an index pointer (i). When you encounter an element that should be moved to the end:
- Remove it using remove(at:)
- Append it to the end of the array.
- Do not increment the index (i) in this case, since a new element has replaced the removed one at the same index.