{"id":9584,"date":"2022-03-28T19:44:45","date_gmt":"2022-03-29T02:44:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9584"},"modified":"2022-03-28T19:45:38","modified_gmt":"2022-03-29T02:45:38","slug":"leetcode-2215-find-the-difference-of-two-arrays","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2215-find-the-difference-of-two-arrays\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2215. Find the Difference of Two Arrays"},"content":{"rendered":"\n<p>Given two&nbsp;<strong>0-indexed<\/strong>&nbsp;integer arrays&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>, return&nbsp;<em>a list<\/em>&nbsp;<code>answer<\/code>&nbsp;<em>of size<\/em>&nbsp;<code>2<\/code>&nbsp;<em>where:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>answer[0]<\/code>&nbsp;<em>is a list of all&nbsp;<strong>distinct<\/strong>&nbsp;integers in<\/em>&nbsp;<code>nums1<\/code>&nbsp;<em>which are&nbsp;<strong>not<\/strong>&nbsp;present in<\/em>&nbsp;<code>nums2<\/code><em>.<\/em><\/li><li><code>answer[1]<\/code>&nbsp;<em>is a list of all&nbsp;<strong>distinct<\/strong>&nbsp;integers in<\/em>&nbsp;<code>nums2<\/code>&nbsp;<em>which are&nbsp;<strong>not<\/strong>&nbsp;present in<\/em>&nbsp;<code>nums1<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that the integers in the lists may be returned in&nbsp;<strong>any<\/strong>&nbsp;order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [1,2,3], nums2 = [2,4,6]\n<strong>Output:<\/strong> [[1,3],[4,6]]\n<strong>Explanation:\n<\/strong>For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].\nFor nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [1,2,3,3], nums2 = [1,1,2,2]\n<strong>Output:<\/strong> [[3],[]]\n<strong>Explanation:\n<\/strong>For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].\nEvery integer in nums2 is present in nums1. Therefore, answer[1] = [].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums1.length, nums2.length &lt;= 1000<\/code><\/li><li><code>-1000 &lt;= nums1[i], nums2[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Use two hashtables to store the unique numbers of array1 and array2 respectfully.<\/p>\n\n\n\n<p>Time complexity: O(m+n)<br>Space complexity: O(m+n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {\n    vector<vector<int>> ans(2);\n    unordered_set<int> s1(begin(nums1), end(nums1));\n    unordered_set<int> s2(begin(nums2), end(nums2));\n    for (int x : s1)\n      if (!s2.count(x)) ans[0].push_back(x);\n    for (int x : s2)\n      if (!s1.count(x)) ans[1].push_back(x);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two&nbsp;0-indexed&nbsp;integer arrays&nbsp;nums1&nbsp;and&nbsp;nums2, return&nbsp;a list&nbsp;answer&nbsp;of size&nbsp;2&nbsp;where: answer[0]&nbsp;is a list of all&nbsp;distinct&nbsp;integers in&nbsp;nums1&nbsp;which are&nbsp;not&nbsp;present in&nbsp;nums2. answer[1]&nbsp;is a list of all&nbsp;distinct&nbsp;integers in&nbsp;nums2&nbsp;which are&nbsp;not&nbsp;present in&nbsp;nums1. Note&nbsp;that the integers&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[772,289,222,82],"class_list":["post-9584","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-arrays","tag-distinct","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9584"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9584\/revisions"}],"predecessor-version":[{"id":9586,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9584\/revisions\/9586"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}