0

Hi,

my question is simply but a little bit tricky; let assume that there are two array

array1 = (2, 5, 6, 1, 7) and array2 (3, 2, 6, 8) and after function setUnion new array or use pointer return (2, 5, 6, 1, 7, 3, 8). Below my code is shown my problem is I found

(2, 5, 6, 1, 7, **8**, **8**) How I fix this problem ? Here my code include main function::

```
#include <iostream>
using namespace std;
void setUnion (const int array1[], const int size1, const int array2[], const int size2, int &newSize, int *&unionArray)
{
newSize = size1+size2;
/** int countDuplicate = 0; */
for (int j = 0; j < size1; ++j)
for (int c = 0; c < size2; ++c)
if (array1[j] == array2[c])
newSize--;
unionArray = new int[newSize];
if (size1 > size2)
{
for (int i = 0; i < size1; ++i)
unionArray[i] = array1[i];
int count = 0;
for (int i = size1; i < newSize; ++i)
for (int j = 0; j < size2; ++j)
for (int c = 0; c < size1; ++c)
if (array1[c] != array2[j])
unionArray[i] = array2[j];
}
else /** size2 > || = size1 */
{
for (int i = 0; i < size2; ++i)
unionArray[i] = array2[i];
for (int i = size2; i < newSize; ++i)
for (int j = 0; j < size2; ++j)
for (int c = 0; c < size1; ++c)
if (array1[c] != array2[j])
unionArray[i] = array1[c];
}
}
int main ()
{
cout<<"TYPE"<<endl;
cout<<endl;
const int size1 = 5;
const int array1[size1] ={2, 5, 6, 1, 7};
const int size2 = 4;
const int array2[size2] = {3, 2, 6, 8};
int newSize;
int* unionArray;
int *pNewSize = &newSize;
setUnion(array1, size1, array2, size2, *pNewSize, unionArray);
cout<<"New size :: "<<newSize<<"or use pointer :: "<<pNewSize<<endl;
for (int i = 0; i < newSize; ++i)
cout<<unionArray[i]<<"\t";
cout<<endl;
return 0;
}
```

pls help me...