So, I have been boggling my mind around this and tried to search here if someone asked this question already.. I'm pretty sure someone has at one point, but I wasn't being able to find it, because I am not really sure how to word it correctly. So, excuse me if this (most likely very simple question) has been answered somewhere already. I still try to specify my problem here, so again, sorry if this is a duplicate. Ok, assume we have the following array of a subset of natural numbers:
var someNumbers = [0, 1, 2, 3, 4];
Or basically any array that is a subset of natural numbers. Now, let's say I modify this array for some reason and have to delete an element from that said array, so I'm getting a new array such as this one:
someNumbers = [0, 1, 3, 4];
This one is missing an element now, the 2, thus the array is no longer a subset of natural numbers. What I'm looking for now is that I basically want to replace each element in this new array with its position, so we would get an array like this again:
someNumbers = [0, 1, 2, 3];
I've had many attempts in trying to accomplish this simple task, and still couldn't figure it out how to do it right. In one instance, I tried to iterate through the array via a for-loop and had it replace each element by comparing the current element as value to the iterator and have that element replaced by the current value of the iterator, just to have it fail at i = 2 since the element "2" is deleted.
Adding a comparison (that would take the missing element in consideration) in the loop and subtracting 1, did help, but would fail if let's say I had deleted element 0 for instance. Furthermore, I'm not even sure if any of my attempts are actually effective and couldn't be done much more effectively.
I just might be severely overthinking this and it's a lot more easier than I think it is.
TL;DR: I basically want to know how to replace each element in the array with its position and how to do it effectively.