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.
can someone please help me figure out whats wrong??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <stdlib.h>

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.