#include #include #include #include using namespace std; // Function to read an array of given size from user input vector readArray(int size) { vector arr(size); for (int i = 0; i < size; i++) { cin >> arr[i]; } return arr; } // Function to compute the union of two arrays, remove duplicates, and sort the result vector getUnion(const vector& arr1, const vector& arr2) { unordered_set unionSet(arr1.begin(), arr1.end()); // Insert all elements from arr1 for (int num : arr2) { unionSet.insert(num); // Insert elements from arr2 } vector result(unionSet.begin(), unionSet.end()); // Convert set to vector sort(result.begin(), result.end()); // Sort the result return result; } int main() { int n, m; // Read size of the first array cout << "Enter size of first array (n): "; cin >> n; // Read size of the second array cout << "Enter size of second array (m): "; cin >> m; // Read elements of the first array cout << "Enter " << n << " elements for the first array: "; vector arr1 = readArray(n); // Read elements of the second array cout << "Enter " << m << " elements for the second array: "; vector arr2 = readArray(m); // Compute union vector unionResult = getUnion(arr1, arr2); // Print the result cout << "Union of the arrays (sorted): "; for (int num : unionResult) { cout << num << " "; } cout << endl; return 0; }