### removing duploicates while merging array's issue

im tryin to merge two sorted int array's without duplicates. the merge function worked well without the duplicate's part but since i added it the merge works but doesnt remove the duplicates. instead, the member's are being added to the array twice with one cell between them.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include #include using namespace std; int* merge(int *a1, int n1, int *a2, int n2) { int i=0, j=0, k=0; int size = n1 + n2; if (size == 0) return NULL; int* arr = (int *)malloc(size*sizeof(int)); while (i < n1 && j < n2) { if ((a1[i] <= a2[j])&&(a1[i]!=arr[k-1])) { arr[k] = a1[i]; i++; k++; } else if (a2[j] != arr[k - 1]) { arr[k] = a2[j]; j++; k++; } else if (a1[i] == arr[k - 1]){ i++; } else{ j++; } } while (i < n1) { arr[k] = a1[i]; i++; k++; } while (j < n2) { arr[k] = a2[j]; j++; k++; } return arr; } int main() { int arr1[] = { 1,3, 3, 5, 7,7, 9 }; int arr2[] = { 2, 4, 6, 8, 10 }; int* arr=merge(arr1, 7, arr2, 5); for (int i = 0; i < 12; i++) { cout << arr[i]; } }``````
Topic archived. No new replies allowed.