Find distinct elements and other operations on the array.

The union of two arrays

The union operation combines two sets, skipping the matching values of both sets while populating the resultant set.

Unlike sets, arrays may contain duplicate values. For example:

  • The union of [2,5,8,9,30,45][2,5,8,9,30,45] and [4,5,6,7,8,9,10][4,5,6,7,8,9,10] should be [2,4,5,8,9,10,30,45][2,4,5,8,9,10,30,45].

  • The union of [2,5,5,8,9,30,45][2,5,5,8,9,30,45] and [4,5,6,7,8,9,10][4,5,6,7,8,9,10] should be [2,4,5,5,8,9,10,30,45][2,4,5,5,8,9,10,30,45].

  • The union of [2,5,5,8,9,30,45][2,5,5,8,9,30,45] and [4,5,5,6,7,8,9,10][4,5,5,6,7,8,9,10] should be [2,4,5,5,8,9,10,30,45][2,4,5,5,8,9,10,30,45].

The following program unites two arrays, assuming that the operand arrays are sorted. This operation may result in duplicate values.

Press + to interact
#include <iostream>
using namespace std;
void print (int arr [], int size) // Function to print an array
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int p[] = {2,5,5,8,8,8,9,30,45}; // Array 1
int q[] = {4,5,5,6,7,7,8,8,9,10,45}; // Array 2
int lq = sizeof(q) / sizeof(q[0]); // Array 2 size
int lp = sizeof(p) / sizeof(p[0]); // Array 1 size
int lr = lp + lq;
int r[lr] = {0};
int i, j, k;
i = j = k = 0;
while (i < lp && j < lq) // This loop will terminate when the values of i,j are not less than lp and lq
{
if (p[i] < q[j]) // If the element of p is less than element of q
{
r[k] = p[i]; // Assigning value of p to r
i++;
}
else // If the element of p is not less than the element of q
{
if(p[i] > q[j]) // If the element of p is greater than the element of q
{
r[k] = q[j]; // Assign the value of q to r
j++;
}
else // The elements of p and q are equal
{
r[k] = p[i];
i++;
j++;
}
}
k++;
}
while (i<lp) // This loop will terminate when the value of i is not less than lp
{
r[k] = p[i];
i++;
k++;
}
while (j<lq) // This loop will terminate when the value of j is not less than lq
{
r[k] = q[j];
j++;
k++;
}
cout << "\n Array 1 is: " ;
print(p,lp);
cout << "\n Array 2 is: ";
print(q,lq);
cout << "\n Resultant array is: ";
print(r,k);
return 0;
}

The above code features a new method of assignment in C++.

  • In i = j = k = 0, all the variables get the value 0. This can also be done in three assignment statements.

In the rest of the code above:

  • The first loop traverses through both arrays (p and q) and puts the smaller value in the resulting r array. After completing this round, any one array is exhausted.
  • The other array still has one or more untraversed values, all of which are certainly greater than or equal to the last value of the exhausted array.
  • Therefore, one of the next two loops executes to add those values to the end of r without comparison.

The intersection of two arrays

The intersection operation gives the matching values of two arrays. Unlike sets, arrays may contain duplicate values. For example:

  • The intersection of [2,5,8,9,30,45][2,5,8,9,30,45] and [4,5,6,7,8,9,10][4,5,6,7,8,9,10] should be [5,8,9][5,8,9].

  • The intersection of [2,5,5,6,8,9,30,45][2,5,5,6,8,9,30,45] and [4,5,6,7,8,8,9,10][4,5,6,7,8,8,9,10] should be [5,6,8,9][5,6,8,9].

  • The intersection of [2,5,5,8,9,30,45][2,5,5,8,9,30,45] and [4,5,5,6,7,8,9,10][4,5,5,6,7,8,9,10] should be [5,5,8,9][5,5,8,9].

The following program assumes that the operand arrays are sorted, and may result in duplicate values:

Press + to interact
#include <iostream>
using namespace std;
void print (int arr [], int size) // Function to print an array
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int p[] = {2,5,5,8,8,8,9,30,45}; // Array 1
int q[] = {4,5,5,6,7,7,8,8,9,10,45}; // Array 2
int lq = sizeof(q) / sizeof(q[0]); // Array 2 size
int lp = sizeof(p) / sizeof(p[0]); // Array 1 size
int lr;
if (lq < lp)
{
lr = lq;
}
else
{
lr = lp;
}
int r[lr] = {0};
int i, j, k;
i = j = k = 0;
while (i < lp && j < lq) // This loop will terminate when the values of i,j are not less than lp and lq
{
if (p[i] == q[j]) // If the element of p is equal to the element of q
{
r[k] = p[i]; // Assigning value of p to k
i++;
j++;
k++;
}
else // If the element of p is not equal to the element of q
{
if(p[i] < q[j]) // If the element of p is less than the element of q
{
i++;
}
else // If the element of p is not less than the element of q
{
j++;
}
}
}
cout << "\n Array 1 is: " ;
print(p,lp);
cout << "\n Array 2 is: ";
print(q,lq);
cout << "\n Resultant array is: ";
print(r,k);
cout << endl;
return 0;
}

Note: If the operand arrays are not sorted already, then the code for sorting them should be added before entering the first loop.

The minus operator for two arrays

The following program implements the minus operator for arrays similar to sets. It’s assumed that the operand arrays are sorted, and the resulting array may contain duplicates, depending upon the contents of the operands.

Press + to interact
#include <iostream>
using namespace std;
void print (int arr [], int size) // Function to print an array
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int p[] = {2,5,5,8,8,8,9,30,35}; // Array 1
int q[] = {4,5,5,6,7,7,8,9,10}; // Array 2
int lq = sizeof(q) / sizeof(q[0]); // Array 1 size
int lp = sizeof(p) / sizeof(p[0]); // Array 2 size
int lr = lp + lq;
int r[lr] = {0}; // Array of size lr
int i, j, k;
i = j = k = 0;
while (i < lp && j < lq) // This loop will terminate when the values of i,j are not less than lp and lq
{
if(p[i] < q[j]) // If the element of p is less than the element of q
{
r[k] = p[i]; // Assigning p to r
i++;
k++;
}
else // If the element of p is not less than the element of q
{
if(p[i] > q[j]) // If the element of p is greater than element of q
{
j++;
}
else // The elements of p and q are equal
{
i++;
j++;
}
}
}
while (i < lp) // This loop will terminate when the value of i is not less than lp
{
r[k] = p[i]; // Assigning to r
i++;
k++;
}
cout << "\n Array 1 is: " ;
print(p,lp);
cout << "\n Array 2 is: ";
print(q,lq);
cout << "\n Resultant array is: ";
print(r,k);
cout << endl;
return 0;
}

Distinct values in a array

The following program generates a new array with the unique values of the operand array:

Press + to interact
#include <iostream>
using namespace std;
void print (int arr [], int size) // Function to print an array
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int p[] = {2,5,5,8,8,8,9,30,45}; // Array that is entered
int lp = sizeof(p) / sizeof(p[0]); // Calculating length of array
int r[lp] = {p[0]}; // Creating an array
int i = 0;
int k = 0;
while (i < lp) // Loop to iterate and make a new array
{
if (p[i] != p[i-1])
{
r[k] = p[i];
k++;
}
i++;
}
cout << "Array that is entered: ";
print(p, lp);
cout << endl;
cout << "Output array: ";
print(r, k);
return 0;
}

Get hands-on with 1400+ tech skills courses.