/请编写函数fun,该函数的功能是:删去一维数组中所有相同的数,
使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数/
#include <stdio.h>
int fun(int arr[], int size)
{
if (size == 0)
{
return 0;
}
int i, j = 0;
for (i = 1; i < size; i++)
{
if (arr[j] != arr[i])
{
arr[++j] = arr[i];
}
}
return j + 1;
}
int main(void)
{
int arr[] = {1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 5}; // 有序数组
int size = sizeof(arr) / sizeof(arr[0]);
int newSize = fun(arr, size);
printf("Array after removing duplicates: ");
for (int i = 0; i < newSize; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
printf("Number of elements after removing duplicates: %d\n", newSize);
return 0;
}
标签:arr,函数,int,数组,fun,size
From: https://www.cnblogs.com/yesiming/p/18280715