Error in Removal of Duplicates from a Sorted Array in C++ using Pointers

  algorithm, arrays, c++, duplicates, pointers

I have a task to remove the duplicates from a sorted array.

However, when I try this it doesn’t remove anything and still gives me the same values in the output as the original.

I think I’m missing something in the removeDuplicates() function.

Also pointer notation would be recommended. Thank you!

void removeDuplicates(int *arr, int *size)
{
    int s,*p,i,k=0;
    p=arr;
    s=*size;
    nt arr1[s];
    for(i=0;i<s-1;i++)
    {
        if (*(p+i)!=*(p+i+1))
        {
            arr1[k++]=*(p+i);
        }
        arr1[k++]=*(p+s-1);
    }

    for(i=0; i<k; i++)
    {
        *(p+i) = arr1[i];
    }
    for(i=0; i<k; i++)
    {
        cout<<*(p+i)<<endl;
    }
}

Source: Windows Questions C++

LEAVE A COMMENT