Q. Remove duplicates from an sorted array inplace. He was expecting a linear time solution for this.
Anónimo
void removeDuplicates(int a[], int n) { int ptr1; int ptr2 = 1; int count = 0; for (ptr1 = 0; ptr1 < n; ptr1++) { // check if ptr2 has reached the end if (a[ptr2] == a[n - 1]) break; while (a[ptr1] == a[ptr2]) { ptr2++; } a[ptr1 + 1] = a[ptr2]; } for (int j = ptr1 + 1; j < n; j++) { a[j] = NULL; } }