From 898a228f24596f5bf2ad0d49dcf9d23e70a3cfb5 Mon Sep 17 00:00:00 2001 From: Rajdeep Roy Chowdhury <32531173+Razdeep@users.noreply.github.com> Date: Fri, 18 Jan 2019 15:07:35 +0530 Subject: [PATCH] Added Solution of 1st problem of DAY22 in C++; Updated README (#188) --- day22/C++/commonElements.cpp | 23 +++++++++++++++++++++++ day22/README.md | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 day22/C++/commonElements.cpp diff --git a/day22/C++/commonElements.cpp b/day22/C++/commonElements.cpp new file mode 100644 index 00000000..7a2c55ab --- /dev/null +++ b/day22/C++/commonElements.cpp @@ -0,0 +1,23 @@ +/** + * @author: Rajdeep Roy Chowdhury + * @github: https://github.com/razdeep + * @date: 18/01/2019 + */ +#include + +int main() +{ + std::vector a = {1, 2, 3, 4, 5, 6, 8, 8, 7}; + std::vector b = {4, 2, 9, 1, 8}; + std::sort(a.begin(), a.end()); + auto last_ptr = std::unique(a.begin(), a.end()); + a.resize(std::distance(a.begin(), last_ptr)); + + for (auto i : a) + { + if (std::find(b.begin(), b.end(), i) != b.end()) + std::cout << i << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/day22/README.md b/day22/README.md index a36ec110..0bfd2f7f 100644 --- a/day22/README.md +++ b/day22/README.md @@ -158,7 +158,34 @@ console.log (searchCommonElements ([0, 12, 41, 20], [9, 3, 1, 5])); ``` *** +## C++ Implementation +### [Solution 1](./C++/commonElements.cpp) +```cpp +/** + * @author: Rajdeep Roy Chowdhury + * @github: https://github.com/razdeep + * @date: 18/01/2019 + */ +#include + +int main() +{ + std::vector a = {1, 2, 3, 4, 5, 6, 8, 8, 7}; + std::vector b = {4, 2, 9, 1, 8}; + std::sort(a.begin(), a.end()); + auto last_ptr = std::unique(a.begin(), a.end()); + a.resize(std::distance(a.begin(), last_ptr)); + + for (auto i : a) + { + if (std::find(b.begin(), b.end(), i) != b.end()) + std::cout << i << std::endl; + } + + return 0; +} +``` ## Question 2 ## JavaScript Implementation