I'm trying to find duplicates in an array and removing them by shifting the array index using the same array. I can't figure out where in my code the problem is but I'm still getting a duplicate. Could someone please help pinpoint the problem? I've tried debugging it but I'm not seeing it.
#include <iostream>
using namespace std;
int removeDuplicates(int A[], int n){
int last = n-1;
int found = 0;
//bool flag = false;
for(int i=0; i < last+1; i++)
{
for(int j=i+1; j < last+1; j++)
{
if(A[i] == A[j])
{
found++;
for(int k = j;k < last+1;k++)
{
A[k]=A[k+1];
//if(A[k-1] == A[i])
// flag = true;
}
last--;
j=j+1;
}
}
}
//found= last-found;
return last;
}
int main() {
// Test data
int n = 24;
int A[] = {1,1,2,4, 1,1,1,2, 3,5,8,0, 2,-2,-4,1, 9,10,-4,11, 11,8,5,-4};
// Call
int last = removeDuplicates(A,n);
// Print Results
for(int i = 0; i <= last; i++)
cout << A[i] << " ";
cout << endl;
return 0;
}