include <iostream>
using namespace std;
int subject1[] = {1,2,2,2,3,3,4,4,4,4,5,7,9}; //result is 7
int subject2[] = {1,1,3,4,5,5,5,5,7,10,10,12,20,20}; //result is 8
int ArrayTrip(int *subject, int len)
{
if (NULL == subject)
{
return 0;
}
if (len < 2)
{
return 1;
}
//快慢指针,慢指针相当于是新的序列,当快指针与慢指针值不同,说明需要将快指针指排入
//新序列,将快指针的值排在慢指针+1位置
//当快慢指针值相同,快指针当前值不需要排入新序列,快指针继续后移
int i = 0;
for (int j = 0; j < len; j++)
{
if (subject[i] != subject[j])
{
i++;
subject[i] = subject[j];
}
}
return i + 1;
}
int main() {
int res = ArrayTrip(subject1, 13);
cout << res << endl;
res = ArrayTrip(subject2, 14);
cout << res << endl;
return 0;
}