Right. Something to consider is that there is a way to avoid this. Hint: Does the problem exist if we do it backwards? And you could also fix what you were doing with a tiny bit of math, but that gets a little messy code, imho.
And what Randy says is true - in fact that is a better approach because manipulating arrays is costly, having to shift all those elements each time - Randyβs approach would avoid that by just creating a new array. But itβs good to understand what is happening with the approach you were trying.