双指针法移除数组元素
#include <stdio.h>
int a[] = {1, 2, 3, 4, 4, 5, 6, 4, 7};
int target = 4;
int main()
{
int slowIndex = 0;
int fastIndex = 0;
int leng = sizeof(a)/sizeof(int);
printf("原数组长度为:%d\n", leng);
for (fastIndex = 0; fastIndex < leng; fastIndex++) {
if (a[fastIndex] != target) {
a[slowIndex++] = a[fastIndex];
}
}
printf("移除后数组长度为:%d\n", slowIndex);
return 0;
}