question about unexpected output

im trying to merge two arrays without duplicates. can someone please tell me why my output includes the number "48" twice??
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <stdlib.h>

using namespace std;

int find_index(int a[], int size, int value)
{
	int i;
	for (i = 0; i<size; i++)
	{
		if (a[i] == value)
		{
			return(value);  
		}
	}
	return(-1);  
}

int* merge_uniq(int *a1, int n1, int *a2, int n2,int* len)
{
	int i = 0, j = 0, k = 0;
	int size = n1 + n2;

	if (size == 0)
	{
		cout << "EMPTY ARRAY" << endl;
		return NULL;
	}

	int* arr = (int *)malloc(size*sizeof(int));

	while (i < n1 && j < n2) {
		if (find_index(arr, size, a1[i]) != -1)
			i++;
		else if (find_index(arr, size, a2[j]) != -1)
			j++;

		else if (a1[i] <= a2[j])
		{
			arr[k] = a1[i];
			i++;
			k++;
		}
		else
		{
			arr[k] = a2[j];
			j++;
			k++;
		}
	}

	while (i < n1)
	{
		arr[k] = a1[i];
		i++;
		k++;
	}

	while (j < n2) 
	{
		arr[k] = a2[j];
		j++;
		k++;
	}

	return arr;
}

void display(int* arr,int size)
{
	for (int i = 0; i < size; i++)
		cout << arr[i]<<" ";
	cout << endl;
}

int main()
{
	int arr1[] = { -15 ,19 ,23 ,23 ,23 ,28 ,35 ,35 ,35 ,48};
	int arr2[] = { -15 ,- 15 ,12 ,23 ,23 ,35 ,48 ,53 ,55 ,55 ,55 };
	MUarr = merge_uniq(arr1, 10, arr2, 11,arr1);
	cout << "Uniq Merged Array: ";
	display(MUarr, 10);
	
}
First, This "find_index" Function you are using will not return the index of an array because you are returning the same value that is passed in as an argument. Return "i" instead of "value".

This is the corrected version of your function.

int find_index(int a[], int size, int value)
{
int i;
for (i = 0; i<size; i++)
{
if (a[i] == value)
{
return i;
}
}
return(-1);
}
thanks for that but it doesnt solve the problem
Topic archived. No new replies allowed.