I am wondering if I can rearrange an array faster when I would like to delete more than one object.
You are given a List:
- 1
- 2
- 2
- 3
- 4
- 5
- 7
And now you want to delete every "2" in the list. So what I would do, is:
int size = 7;
int array[size] = {1,2,2,3,4,5,7};
for(int i = size-1; i >= 0; i--){
if(array[i] == 2){
memcpy(&array[i],&array[i+1], size-i);
size--;
}
}
But this is not a efficent way doing so, is it? So I wonder if I could do this on a smarter way without allocating extra memory.