C++代码:
#include<iostream>
using namespace std;
int removeDuplicates(int *A, int n) {
if (A == NULL) {
return -1;
}
int count = 0;
for (int i = 1; i<n; i++) {
if (A[i] == A[i - 1]) {
count++;
if (count>1) {
for (int j = i; j<n - 1; j++) {
A[j] = A[j + 1];
}
n--;
i--;
}
}
else {
count = 0;
}
}
return n;
}
int main() {
int A[100];
int n,num;
cout << "Please input the number of array:";
cin >> n;
cout << "Please input the array:";
for (int i = 0; i < n; i++) {
cin >> A[i];
}
num = removeDuplicates(A, n);
cout << "The number of array after remove duplicates:" << num << endl;
cout << "The array after remove duplicates:";
for (int j = 0; j < num; j++) {
cout << A[j] << " ";
}
cout << endl;
return 0;
}
C++运行结果: