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 and should be .
-
The union of and should be .
-
The union of and should be .
The following program unites two arrays, assuming that the operand arrays are sorted. This operation may result in duplicate values.
#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 1int q[] = {4,5,5,6,7,7,8,8,9,10,45}; // Array 2int lq = sizeof(q) / sizeof(q[0]); // Array 2 sizeint lp = sizeof(p) / sizeof(p[0]); // Array 1 sizeint 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 ri++;}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 rj++;}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 value0
. This can also be done in three assignment statements.
In the rest of the code above:
- The first loop traverses through both arrays (
p
andq
) and puts the smaller value in the resultingr
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 and should be .
-
The intersection of and should be .
-
The intersection of and should be .
The following program assumes that the operand arrays are sorted, and may result in duplicate values:
#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 1int q[] = {4,5,5,6,7,7,8,8,9,10,45}; // Array 2int lq = sizeof(q) / sizeof(q[0]); // Array 2 sizeint lp = sizeof(p) / sizeof(p[0]); // Array 1 sizeint 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 ki++;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.
#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 1int q[] = {4,5,5,6,7,7,8,9,10}; // Array 2int lq = sizeof(q) / sizeof(q[0]); // Array 1 sizeint lp = sizeof(p) / sizeof(p[0]); // Array 2 sizeint lr = lp + lq;int r[lr] = {0}; // Array of size lrint 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 ri++;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 ri++;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:
#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 enteredint lp = sizeof(p) / sizeof(p[0]); // Calculating length of arrayint r[lp] = {p[0]}; // Creating an arrayint 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.