For the life of me I cant figure out this simple task

I know with a static array you just simple shift over one element and decrease the size in variable pertaining the length of the array as such

for(i = 0; i < n; i++)

if(key == array[i])
  arr[i] = arr[i+1];


but in the dynamic array you want to of course deallocate the item you remove, hence the point of dynamic allocation dont take up the space if your not using it

Edited 7 Years Ago by power_computer: n/a

You can do exactly the same as you have described for your statically allocated array, then create a new smaller array and copy all of your remaining elements to it (This is an inefficient and perhaps slightly naive solution, but its a simple one)

Yes I did exactly as you said, somewhat, but then got to thinking in a dynamic array you the length and a max size, the max size will only change if the length is meet or greater than it so it can allocate more room for more elements, so therefore the size of the array just like an static array is relavant to the max size of the array not length so in all i just did the same as a static array shifted all elements over so the elementtobedeleted is last and decremented the length because it still seems to me the total memory of the array pertains to the current max of the array, unless you set some function to reduce max size if you have some percent of elements drastically less than the max, sorry if i confused you

This question has already been answered. Start a new discussion instead.