TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
intersection_of_two_arrays.cpp
Go to the documentation of this file.
1
14#include <algorithm>
15#include <cassert>
16#include <iostream>
17#include <vector>
18
24
31void print(const std::vector<int32_t> &array) {
32 for (int32_t i : array) {
33 std::cout << i << " ";
34 }
35 std::cout << "\n";
36}
37
49std::vector<int32_t> get_intersection(const std::vector<int32_t> &first,
50 const std::vector<int32_t> &second) {
51 std::vector<int32_t> res;
52 size_t f_index = 0;
53 size_t s_index = 0;
54 size_t f_length = first.size();
55 size_t s_length = second.size();
56
57 while (f_index < f_length && s_index < s_length) {
58 if (first[f_index] < second[s_index]) {
59 f_index++;
60 } else if (first[f_index] > second[s_index]) {
61 s_index++;
62 } else {
63 if ((res.size() == 0) || (first[f_index] != res.back())) {
64 res.push_back(
65 first[f_index]);
66 }
67 f_index++;
68 s_index++;
69 }
70 }
71 return res;
72}
73
74} // namespace operations_on_datastructures
75
80namespace tests {
87void test1() {
88 std::cout << "TEST CASE 1\n";
89 std::cout << "Intialized a = {} b = {}\n";
90 std::cout << "Expected result: {}\n";
91 std::vector<int32_t> a = {};
92 std::vector<int32_t> b = {};
93 std::vector<int32_t> result = get_intersection(a, b);
94 assert(result == a);
95 print(result);
96 std::cout << "TEST PASSED!\n\n";
97}
102void test2() {
103 std::cout << "TEST CASE 2\n";
104 std::cout << "Intialized a = {} b = {2, 3}\n";
105 std::cout << "Expected result: {}\n";
106 std::vector<int32_t> a = {};
107 std::vector<int32_t> b = {2, 3};
108 std::vector<int32_t> result = get_intersection(a, b);
109 assert(result == a);
110 print(result);
111 std::cout << "TEST PASSED!\n\n";
112}
117void test3() {
118 std::cout << "TEST CASE 3\n";
119 std::cout << "Intialized a = {4, 6} b = {3, 6}\n";
120 std::cout << "Expected result: {6}\n";
121 std::vector<int32_t> a = {4, 6};
122 std::vector<int32_t> b = {3, 6};
123 std::vector<int32_t> result = get_intersection(a, b);
124 std::vector<int32_t> expected = {6};
125 assert(result == expected);
126 print(result);
127 std::cout << "TEST PASSED!\n\n";
128}
133void test4() {
134 std::cout << "TEST CASE 4\n";
135 std::cout << "Intialized a = {4, 6, 6, 6} b = {2, 4, 4, 6}\n";
136 std::cout << "Expected result: {4, 6}\n";
137 std::vector<int32_t> a = {4, 6, 6, 6};
138 std::vector<int32_t> b = {2, 4, 4, 6};
139 std::vector<int32_t> result = get_intersection(a, b);
140 std::vector<int32_t> expected = {4, 6};
141 assert(result == expected);
142 print(result);
143 std::cout << "TEST PASSED!\n\n";
144}
149void test5() {
150 std::cout << "TEST CASE 5\n";
151 std::cout << "Intialized a = {1, 2, 3, 4, 6, 7, 9} b = {2, 3, 4, 5}\n";
152 std::cout << "Expected result: {2, 3, 4}\n";
153 std::vector<int32_t> a = {1, 2, 3, 4, 6, 7, 9};
154 std::vector<int32_t> b = {2, 3, 4, 5};
155 std::vector<int32_t> result = get_intersection(a, b);
156 std::vector<int32_t> expected = {2, 3, 4};
157 assert(result == expected);
158 print(result);
159 std::cout << "TEST PASSED!\n\n";
160}
166void test6() {
167 std::cout << "TEST CASE 6\n";
168 std::cout << "Intialized a = {1, 3, 3, 2, 5, 9, 4, 7, 3, 2} ";
169 std::cout << "b = {11, 3, 7, 8, 6}\n";
170 std::cout << "Expected result: {3, 7}\n";
171 std::vector<int32_t> a = {1, 3, 3, 2, 5, 9, 4, 7, 3, 2};
172 std::vector<int32_t> b = {11, 3, 7, 8, 6};
173 std::sort(a.begin(), a.end());
174 std::sort(b.begin(), b.end());
175 std::vector<int32_t> result = get_intersection(a, b);
176 std::vector<int32_t> expected = {3, 7};
177 assert(result == expected);
178 print(result);
179 std::cout << "TEST PASSED!\n\n";
180}
181} // namespace tests
182
187static void test() {
188 tests::test1();
189 tests::test2();
190 tests::test3();
191 tests::test4();
192 tests::test5();
193 tests::test6();
194}
195
200int main() {
201 test(); // run self-test implementations
202 return 0;
203}
uint64_t result(uint64_t n)
static void test()
Function to test the correctness of get_intersection() function.
int main()
main function
void print(const std::vector< int32_t > &array)
Prints the values of a vector sequentially, ending with a newline character.
std::vector< int32_t > get_intersection(const std::vector< int32_t > &first, const std::vector< int32_t > &second)
Gets the intersection of two sorted arrays, and returns them in a vector.
Testcases to check Union of Two Arrays.
void test1()
A Test to check an simple case.
void test4()
A Test to check a very large input.
void test3()
A Test to check an invalid shift value.
void test6()
A Test to check correct functionality with an array sorted using std::sort.
void test2()
A Test to check an empty vector.
void test5()
A Test to check a shift of zero.