|
| 1 | +# 2215. Find the Difference of Two Arrays |
| 2 | + |
| 3 | +- Difficulty: Easy. |
| 4 | +- Related Topics: Array, Hash Table. |
| 5 | +- Similar Questions: Intersection of Two Arrays, Intersection of Two Arrays II, Intersection of Multiple Arrays. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Given two **0-indexed** integer arrays `nums1` and `nums2`, return **a list** `answer` **of size** `2` **where:** |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- `answer[0]` **is a list of all **distinct** integers in** `nums1` **which are **not** present in** `nums2`**.** |
| 14 | + |
| 15 | +- `answer[1]` **is a list of all **distinct** integers in** `nums2` **which are **not** present in** `nums1`. |
| 16 | + |
| 17 | + |
| 18 | +**Note** that the integers in the lists may be returned in **any** order. |
| 19 | + |
| 20 | + |
| 21 | +Example 1: |
| 22 | + |
| 23 | +``` |
| 24 | +Input: nums1 = [1,2,3], nums2 = [2,4,6] |
| 25 | +Output: [[1,3],[4,6]] |
| 26 | +Explanation: |
| 27 | +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]. |
| 28 | +For 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]. |
| 29 | +``` |
| 30 | + |
| 31 | +Example 2: |
| 32 | + |
| 33 | +``` |
| 34 | +Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2] |
| 35 | +Output: [[3],[]] |
| 36 | +Explanation: |
| 37 | +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]. |
| 38 | +Every integer in nums2 is present in nums1. Therefore, answer[1] = []. |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +**Constraints:** |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +- `1 <= nums1.length, nums2.length <= 1000` |
| 47 | + |
| 48 | +- `-1000 <= nums1[i], nums2[i] <= 1000` |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## Solution |
| 53 | + |
| 54 | +```javascript |
| 55 | +/** |
| 56 | + * @param {number[]} nums1 |
| 57 | + * @param {number[]} nums2 |
| 58 | + * @return {number[][]} |
| 59 | + */ |
| 60 | +var findDifference = function(nums1, nums2) { |
| 61 | + var map = {}; |
| 62 | + for (var i = 0; i < nums1.length; i++) { |
| 63 | + map[nums1[i]] = (map[nums1[i]] || 0) | 1; |
| 64 | + } |
| 65 | + for (var i = 0; i < nums2.length; i++) { |
| 66 | + map[nums2[i]] = (map[nums2[i]] || 0) | 2; |
| 67 | + } |
| 68 | + var res = [new Set(), new Set()]; |
| 69 | + for (var i = 0; i < nums1.length; i++) { |
| 70 | + if (!(map[nums1[i]] & 2)) { |
| 71 | + res[0].add(nums1[i]); |
| 72 | + } |
| 73 | + } |
| 74 | + for (var i = 0; i < nums2.length; i++) { |
| 75 | + if (!(map[nums2[i]] & 1)) { |
| 76 | + res[1].add(nums2[i]); |
| 77 | + } |
| 78 | + } |
| 79 | + return res.map(item => Array.from(item)); |
| 80 | +}; |
| 81 | +``` |
| 82 | + |
| 83 | +**Explain:** |
| 84 | + |
| 85 | +nope. |
| 86 | + |
| 87 | +**Complexity:** |
| 88 | + |
| 89 | +* Time complexity : O(n). |
| 90 | +* Space complexity : O(n). |
0 commit comments