Skip to content

Commit

Permalink
Prob: 180 intersection of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Jan 20, 2018
1 parent c942fb6 commit e1b6bcc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 179 |
| Total Problems | 180 |

</center>

Expand Down Expand Up @@ -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)|
48 changes: 48 additions & 0 deletions leet_code_problems/intersection_of_array.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <unordered_set>

std::vector<int> find_intersection(const std::vector<int>& vec1,
const std::vector<int>& vec2)
{
std::vector<int> result;
std::unordered_set<int> set(vec1.begin(), vec1.end());
for (int num : vec2) {
if (set.erase(num)) {
result.push_back(num);
}
}
return result;
}

template <typename T>
void print_vector(const std::vector<T>& vec) {
for (auto n: vec) {
std::cout << n << " ";
}
std::cout << std::endl;
}

int main()
{
std::vector<int> vec1 {1, 2, 3, 3, 4, 4, 5, 5};
std::vector<int> vec2 {2, 3, 5, 5, 4, 4, 6};
std::cout << "Vec 1: ";
print_vector(vec1);
std::cout << "Vec 2: ";
print_vector(vec2);
std::vector<int> result = find_intersection(vec1, vec2);
std::cout << "Result: ";
print_vector(result);
return 0;
}

0 comments on commit e1b6bcc

Please sign in to comment.