TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
union_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_union(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 int32_t next = 0;
57
58 while (f_index < f_length && s_index < s_length) {
59 if (first[f_index] < second[s_index]) {
60 next = first[f_index];
61 f_index++;
62 } else if (first[f_index] > second[s_index]) {
63 next = second[s_index];
64 s_index++;
65 } else {
66 next = first[f_index];
67 f_index++;
68 s_index++;
69 }
70 if ((res.size() == 0) || (next != res.back())) {
71 res.push_back(next);
72 }
73 }
74 while (f_index < f_length) {
75 next = first[f_index];
76 if ((res.size() == 0) || (next != res.back())) {
77 res.push_back(next);
78 }
79 f_index++;
80 }
81 while (s_index < s_length) {
82 next = second[s_index];
83 if ((res.size() == 0) || (next != res.back())) {
84 res.push_back(next);
85 }
86 s_index++;
87 }
88 return res;
89}
90
91} // namespace operations_on_datastructures
92
97namespace tests {
104void test1() {
105 std::cout << "TEST CASE 1\n";
106 std::cout << "Intialized a = {} b = {}\n";
107 std::cout << "Expected result: {}\n";
108 std::vector<int32_t> a = {};
109 std::vector<int32_t> b = {};
110 std::vector<int32_t> result = get_union(a, b);
111 assert(result == a);
112 print(result);
113 std::cout << "TEST PASSED!\n\n";
114}
119void test2() {
120 std::cout << "TEST CASE 2\n";
121 std::cout << "Intialized a = {} b = {2, 3}\n";
122 std::cout << "Expected result: {2, 3}\n";
123 std::vector<int32_t> a = {};
124 std::vector<int32_t> b = {2, 3};
125 std::vector<int32_t> result = get_union(a, b);
126 assert(result == b);
127 print(result);
128 std::cout << "TEST PASSED!\n\n";
129}
134void test3() {
135 std::cout << "TEST CASE 3\n";
136 std::cout << "Intialized a = {4, 6} b = {2, 3}\n";
137 std::cout << "Expected result: {2, 3, 4, 6}\n";
138 std::vector<int32_t> a = {4, 6};
139 std::vector<int32_t> b = {2, 3};
140 std::vector<int32_t> result = get_union(a, b);
141 std::vector<int32_t> expected = {2, 3, 4, 6};
142 assert(result == expected);
143 print(result);
144 std::cout << "TEST PASSED!\n\n";
145}
150void test4() {
151 std::cout << "TEST CASE 4\n";
152 std::cout << "Intialized a = {4, 6, 6, 7} b = {2, 3, 4}\n";
153 std::cout << "Expected result: {2, 3, 4, 6, 7}\n";
154 std::vector<int32_t> a = {4, 6, 6, 7};
155 std::vector<int32_t> b = {2, 3, 4};
156 std::vector<int32_t> result = get_union(a, b);
157 std::vector<int32_t> expected = {2, 3, 4, 6, 7};
158 assert(result == expected);
159 print(result);
160 std::cout << "TEST PASSED!\n\n";
161}
166void test5() {
167 std::cout << "TEST CASE 5\n";
168 std::cout << "Intialized a = {1, 4, 6, 7, 9} b = {2, 3, 5}\n";
169 std::cout << "Expected result: {1, 2, 3, 4, 5, 6, 7, 9}\n";
170 std::vector<int32_t> a = {1, 4, 6, 7, 9};
171 std::vector<int32_t> b = {2, 3, 5};
172 std::vector<int32_t> result = get_union(a, b);
173 std::vector<int32_t> expected = {1, 2, 3, 4, 5, 6, 7, 9};
174 assert(result == expected);
175 print(result);
176 std::cout << "TEST PASSED!\n\n";
177}
183void test6() {
184 std::cout << "TEST CASE 6\n";
185 std::cout << "Intialized a = {1, 3, 3, 2, 5, 9, 4, 3, 2} ";
186 std::cout << "b = {11, 3, 7, 8, 6}\n";
187 std::cout << "Expected result: {1, 2, 3, 4, 5, 6, 7, 8, 9, 11}\n";
188 std::vector<int32_t> a = {1, 3, 3, 2, 5, 9, 4, 3, 2};
189 std::vector<int32_t> b = {11, 3, 7, 8, 6};
190 std::sort(a.begin(), a.end());
191 std::sort(b.begin(), b.end());
192 std::vector<int32_t> result = get_union(a, b);
193 std::vector<int32_t> expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11};
194 assert(result == expected);
195 print(result);
196 std::cout << "TEST PASSED!\n\n";
197}
198} // namespace tests
199
204static void test() {
205 tests::test1();
206 tests::test2();
207 tests::test3();
208 tests::test4();
209 tests::test5();
210 tests::test6();
211}
212
217int main() {
218 test(); // run self-test implementations
219 return 0;
220}
uint64_t result(uint64_t n)
std::vector< int32_t > get_union(const std::vector< int32_t > &first, const std::vector< int32_t > &second)
Gets the union of two sorted arrays, and returns them in a vector.
void print(const std::vector< int32_t > &array)
Prints the values of a vector sequentially, ending with a newline character.
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.
static void test()
Function to test the correctness of get_union() function.
int main()
main function