Skip to content

Commit 52e10ff

Browse files
authored
Create find-the-difference-of-two-arrays.cpp
1 parent b24b904 commit 52e10ff

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// hash table
5+
class Solution {
6+
public:
7+
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
8+
vector<vector<int>> result(2);
9+
vector<unordered_set<int>> lookup = {{cbegin(nums1), cend(nums1)}, {cbegin(nums2), cend(nums2)}};
10+
for (int i = 0; i < 2; ++i) {
11+
for (const auto& x : lookup[i]) {
12+
if (!lookup[1 - i].count(x)) {
13+
result[i].emplace_back(x);
14+
}
15+
}
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)