当然,先排序再删除肯定可以。
现在说的情况是就是无序的状态,不给排序删除。时间复杂度有点高,可以优化。如有错误请告知本人啊!!!
代码如下:
#include<stdio.h>
int elimination(int a[], int count){ //去除数组中重复的元素
int i, j, k;
for (i = 0; i < count; i++){
for (j = i + 1; j < count; j++){
if (a[j] == a[i]){
for (k = j + 1; k< count; k++)
a[k - 1] = a[k];
count--;
j--;
}
}
}
return count;
}
void main(){
int a[] = { 9, 8, 6, 8, 9,9, 3, 2, 6, 5, 4, 7, 1, 1, 9,6 };
int count = elimination(a, 16);
printf("%d\n", count);
for (int i = 0; i < count; i++)
printf("%d ", a[i]);
}