Visual C++ - Union of Two Arrays
Here is the Visual C++ source code for union of two arrays which means finding the union of elements between two arrays. The good thing about this coding is it will allocate only the required memory. During the first iterator it will count how many number of unique elements are available in two arrays. Then use the formula
"array1 unique count + array2 unique count - intersection of array1 and array2 elements count"
to allocate the memory and fill in the elements in its second iteration.
Source Code
#include <stdio.h>
#include <iostream>
int array1[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
95, 85, 75, 65, 55, 45, 35, 25, 15, 05,
10, 15, 20, 25, 30, 35, 40, 45, 50, 55
};
int array2[] = { 15, 25, 35, 45, 55,
12, 22, 32, 43, 52,
15, 25, 35, 45, 55
};
typedef struct _Array
{
int *elements;
int len;
_Array()
{
elements = NULL;
len = 0;
}
_Array(int *data, int len)
{
this->elements = new int[len];
this->len = len;
for(int i = 0; i < len; i++)
this->elements[i] = data[i];
}
_Array(int len)
{
this->len = len;
this->elements = new int[len];
}
~_Array()
{
if(this->elements != NULL)
{
delete [] this->elements;
this->elements = NULL;
this->len = 0;
}
}
} Array;
// Find the array Intersection = Common Elements from two arrays
Array* Find_Common_Elements(Array *p1, Array *p2)
{
int count = 0;
for(int i = 0; i < p1->len; i++)
{
for(int j = 0; j < p2->len; j++)
{
if(p1->elements[i] == p2->elements[j])
{
count++;
break;
}
}
}
Array *result = new Array(count);
count = 0;
for(int i = 0; i < p1->len; i++)
{
for(int j = 0; j < p2->len; j++)
{
if(p1->elements[i] == p2->elements[j])
{
result->elements[count++] = p1->elements[i];
break;
}
}
}
return result;
}
Array* BubbleSort(Array *p)
{
for(int i = 1; i < p->len; i++)
{
for(int j = 0; j < p->len - i; j++)
{
if(p->elements[j] > p->elements[j + 1])
{
int temp = p->elements[j];
p->elements[j] = p->elements[j + 1];
p->elements[j + 1] = temp;
}
}
}
return p;
}
// Union of two unique arrays
Array* Union_Array(Array *p1, Array *p2)
{
int count = p1->len + p2->len;
Array *temp = Find_Common_Elements(p1, p2);
count = count - temp->len;
Array *result = new Array(count);
count = 0;
for(int i = 0; i < p1->len; i++)
result->elements[count++] = p1->elements[i];
for(int j = 0; j < p2->len; j++)
{
bool bInclude = true;
for(int k = 0; k < temp->len; k++)
{
if(temp->elements[k] == p2->elements[j])
{
bInclude = false;
break;
}
}
if(bInclude == true)
result->elements[count++] = p2->elements[j];
}
return BubbleSort(result);
}
Array* Find_Unique_Elements(Array *p)
{
BubbleSort(p);
int element = p->elements[0];
int count = 1;
for(int i = 1; i < p->len; i++)
{
if(element == p->elements[i])
continue;
else
{
element = p->elements[i];
count++;
}
}
Array *result = new Array(count);
count = 0;
element = p->elements[0];
result->elements[count++] = element;
for(int i = 1; i < p->len; i++)
{
if(element == p->elements[i])
continue;
else
{
element = p->elements[i];
result->elements[count++] = element;
}
}
return result;
}
int main()
{
Array *a1 = new Array(array1, sizeof(array1) / sizeof(array1[0]));
Array *a2 = new Array(array2, sizeof(array2) / sizeof(array2[0]));
Array *p1 = Find_Unique_Elements(a1);
Array *p2 = Find_Unique_Elements(a2);
Array *result = Union_Array(p1, p2);
std::cout << "\n\nUnique Sorted Elements in Array1: ";
for(int i = 0; i < p1->len; i++)
std::cout << p1->elements[i] << " ";
std::cout << "\n\nUnique Sorted Elements in Array2: ";
for(int i = 0; i < p2->len; i++)
std::cout << p2->elements[i] << " ";
std::cout << "\n\nUnion of Elements in Array1 and Array2: ";
for(int i = 0; i < result->len; i++)
std::cout << result->elements[i] << " ";
std::cout << "\n\n";
delete a1, a2, p1, p2, result;
return 0;
}
Output
|