From 3d4845a30a16c6cee87350c5dbe41a5ddc3965f4 Mon Sep 17 00:00:00 2001 From: Aakrisht69 <115407142+Aakrisht69@users.noreply.github.com> Date: Thu, 20 Oct 2022 00:34:35 +0530 Subject: [PATCH] Create Intersection.cpp Hacktoberfest Submission. #13 --- Cpp/Intersection.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Cpp/Intersection.cpp diff --git a/Cpp/Intersection.cpp b/Cpp/Intersection.cpp new file mode 100644 index 0000000..7e531e0 --- /dev/null +++ b/Cpp/Intersection.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +static void UnionArray(int arr1[], int arr2[], int l1, + int l2) +{ + // Taking max element present in either array + int m = arr1[l1 - 1]; + int n = arr2[l2 - 1]; + int ans = 0; + if (m > n) + ans = m; + else + ans = n; + + // Finding elements from 1st array (non duplicates + // only). Using another array for storing union elements + // of both arrays Assuming max element present in array + // is not more than 10^7 + int newtable[ans + 1]; + memset(newtable, 0, sizeof(newtable)); + // First element is always present in final answer + cout << arr1[0] << " "; + + // Incrementing the First element's count in it's + // corresponding index in newtable + ++newtable[arr1[0]]; + + // Starting traversing the first array from 1st index + // till last + for (int i = 1; i < l1; i++) { + // Checking whether current element is not equal to + // it's previous element + if (arr1[i] != arr1[i - 1]) { + cout << arr1[i] << " "; + ++newtable[arr1[i]]; + } + } + + // Finding only non common elements from 2nd array + for (int j = 0; j < l2; j++) { + // By checking whether it's already resent in + // newtable or not + if (newtable[arr2[j]] == 0) { + cout << arr2[j] << " "; + ++newtable[arr2[j]]; + } + } +} + +// Driver Code +int main() +{ + int arr1[] = { 1, 2, 2, 2, 3 }; + int arr2[] = { 2, 3, 4, 5 }; + int n = sizeof(arr1) / sizeof(arr1[0]); + int m = sizeof(arr2) / sizeof(arr2[0]); + + UnionArray(arr1, arr2, n, m); + + return 0; +}