1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51



int RemoveDuplicates(int A[], int n) 
{
    if (n < 1) return 0;

    int j = 0;
    int num = A[0];
    int count = 1;
    int i = 1;

    while(++j < n)
        if (num == A[j] && count < 2)
        {
            A[i++] = A[j];
            count = 2;
        }
        else if(num != A[j])
        {
            num = A[i++] = A[j];
            count = 1;
        }

    return i;

}

#include <iostream>

using namespace std;


void PrintArray(int A[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << A[i] << ',';
    cout << endl;
}

int main(int argc, char** argv)
{
    int A[] = {1,1,1,1,1,1,2,2,3,3,3,3,3,3,4,5,6,7,8,8,8,9,9,10,10};

    int n = sizeof(A)/sizeof(int);
    PrintArray(A, n);

    int x = RemoveDuplicates(A, n);
    PrintArray(A, x);
    
    return 0;
}
View Program Text


Test Status