Removing a value is much more challenging than adding a value, but will be great for seeing how to work with arrays.
We can’t actually remove an element from an array. Instead, we move data around within the array, and adjust the number of elements we are using to have the same effect. There are three options for removing an element, as shown in the image below. All the options relate to how we retain the existing data when replacing the element being removed.
Option 1 - move the last element
The easy way to do this would be to swap the last element of the array into the location where the element to be removed is. Then reduce the size, so that the old last element is no longer considered to be part of the data.
Once we have the index, we can validate it to ensure it is within the appropriate range. If not, we can return with an error message. The removal itself is then simply the process of copying the data from the last element to the index we want to remove and reducing the size. As you can see in the image above, the 7 is removed and the 1 is retained, just in a new location in the array.
Have a go at coding this option. Remember to call it in main, and test it out with a few values.
Option 2 - Shifting elements one by one
If you want to keep the order the same, then we need to shift more values around. In fact, we need to move each value that occurs after the element we are removing back one position in the array.
Most of the removal logic would remain the same, but now we need a for loop to loop for each element after index. Within that loop we can copy the values back one position in the array. Here we need to work with two positions in the array at the same time. As they are adjacent to each other we can use i and i - 1. Where i is the current element in the array (from the perspective of the for loop) and i - 1 is one position earlier in the array. Similarly, we can use i + 1 if we wanted the next position in the array.
Any time you are using something like i + 1 or i - 1 to access an array element, you need to think about the array boundaries. You want to make sure your code can never go past the end of the array (back past the start, or on past the end). In our case, we start at index + 1 and index much be >= 0. Therefore, i - 1 will never read past the start of the array.
Have a go at coding this option. Comment out your original code so that you can keep a record of this and go back to it if you want to play with it further.
Make sure to test this out before moving on.
Option 3 - using memory copy functions
What we have so far is effective, but it can be more efficient.
In C/C++, the standard library provides a memcpy function. We can use this to copy the memory efficiently. In this case, it can copy all the elements after the removed element, back over the elements starting at the element being removed. This is the same idea as option 2, just using the memory copy function we saw in copying arrays. In this case we only copy part of the array, and copying it over the array itself.
The following pseudocode shows how to do this. We need to calculate the number of elements to copy, and use this to determine the number of bytes to copy.
Have a go at this yourself.
Summary
Remember the point of this exercise has been to work through the actions you can perform with arrays. Things like adding a removing elements work the same regardless of the kind of data stored in the array. Understanding how this works using basic data types now, will mean that you can easily work with other data types going forward.