diff --git a/README.md b/README.md index 60797e8..1d4646b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ | Current Status| Stats | | :------------: | :----------: | -| Total Problems | 179 | +| Total Problems | 180 | @@ -256,3 +256,4 @@ Include contains single header implementation of data structures and some algori | Find median from a data stream. Design a data structure that supports addNum to add a number to the stream, and findMedian to return the median of the current numbers seen so far. Also, if the count of numbers is even, return average of two middle elements, return median otherwise.|[median_stream.cpp](leet_code_problems/median_stream.cpp) | Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ) | [remove_invalid_parenthesis.cpp](leet_code_problems/remove_invalid_parenthesis.cpp)| | Given an array and a value, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length.| [remove_element.cpp](leet_code_problems/remove_element.cpp)| +| Find intersection of two arrays/vectors, Given two vectors find the result of their interaction. The result should only contain unique characters and can be in any order|[intersection_of_array.cpp](leet_code_problems/intersection_of_array.cpp)| diff --git a/leet_code_problems/intersection_of_array.cpp b/leet_code_problems/intersection_of_array.cpp new file mode 100644 index 0000000..85dc287 --- /dev/null +++ b/leet_code_problems/intersection_of_array.cpp @@ -0,0 +1,48 @@ +/* + * Find intersection of two arrays/vectors: + * Given two vectors find the result of their interaction. + * The result should only contain unique characters and can be in any order. + * + * [1 2 3 3 4 4 5 5] + * [2 3 5 5 4 4 6] + * Result: [2 3 5 4] or [2 3 4 5] + */ + +#include +#include +#include + +std::vector find_intersection(const std::vector& vec1, + const std::vector& vec2) +{ + std::vector result; + std::unordered_set set(vec1.begin(), vec1.end()); + for (int num : vec2) { + if (set.erase(num)) { + result.push_back(num); + } + } + return result; +} + +template +void print_vector(const std::vector& vec) { + for (auto n: vec) { + std::cout << n << " "; + } + std::cout << std::endl; +} + +int main() +{ + std::vector vec1 {1, 2, 3, 3, 4, 4, 5, 5}; + std::vector vec2 {2, 3, 5, 5, 4, 4, 6}; + std::cout << "Vec 1: "; + print_vector(vec1); + std::cout << "Vec 2: "; + print_vector(vec2); + std::vector result = find_intersection(vec1, vec2); + std::cout << "Result: "; + print_vector(result); + return 0; +} \ No newline at end of file