1
Answer

Problem with Code for Removing Duplicates from an Array

Photo of Ashley Singh

Ashley Singh

2y
770
1

I'm trying to use the code provided in this article to remove duplicates from an array, but I'm running into some problems.

Here's the code I'm using:

```

int removeDuplicates(int arr[], int n)
{
if (n==0 || n==1)
return n;

int j = 0;

for (int i=0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j++] = arr[i];

arr[j++] = arr[n-1];

return j;
}
```

When I run this code, I'm getting an "array index out of bounds" error. I'm not sure what's causing the issue. Has anyone else had this issue before? Any help would be appreciated. Thanks!

Answers (1)