2215.Find the Difference of Two Arrays === ###### tags: `Easy`,`Array`,`Hash Table` [2215. Find the Difference of Two Arrays](https://leetcode.com/problems/find-the-difference-of-two-arrays/) ### 題目描述 Given two **0-indexed** integer arrays `nums1` and `nums2`, return *a list* `answer` *of size* `2` *where:* * `answer[0]` *is a list of all **distinct** integers in* `nums1` *which are **not** present in* `nums2`. * `answer[1]` *is a list of all **distinct** integers in* `nums2` *which are **not** present in* `nums1`. **Note** that the integers in the lists may be returned in **any** order. ### 範例 **Example 1:** ``` Input: nums1 = [1,2,3], nums2 = [2,4,6] Output: [[1,3],[4,6]] Explanation: 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]. 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]. ``` **Example 2:** ``` Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2] Output: [[3],[]] Explanation: 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]. Every integer in nums2 is present in nums1. Therefore, answer[1] = []. ``` **Constraints**: * 1 <= `nums1.length`, `nums2.length` <= 1000 * -1000 <= `nums1[i]`, `nums2[i]` <= 1000 ### 解答 #### TypeScript ```typescript= function findDifference(nums1: number[], nums2: number[]): number[][] { const set1 = new Set(nums1); const set2 = new Set(nums2); set1.forEach((item) => { if (set2.has(item)) { set2.delete(item); set1.delete(item); } }); return [[...set1], [...set2]]; } ``` > [name=Sheep][time=Wed, May 3, 2023] #### Javascript ```javascript= function findDifference(nums1, nums2) { const set1 = new Set(nums1); const set2 = new Set(nums2); for (const item of set1) { if (set2.has(item)) { set1.delete(item); set2.delete(item); } } return [[...set1], [...set2]]; } ``` > 慚愧,跟我弟學習了,本來用set1去刪set2然後再用set2刪set1,不止要做兩遍還要多建一個set根本87 > [name=Marsgoat][time=Wed, May 3, 2023] #### Python ```python= class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: return [list(set(nums1) - set(nums2)), list(set(nums2) - set(nums1))] ``` > [name=Ron Chen][time=Wed, May 3, 2023] #### C# ```csharp= public IList<IList<int>> FindDifference(int[] nums1, int[] nums2) { HashSet<int> set1 = new HashSet<int>(nums1); HashSet<int> set2 = new HashSet<int>(nums2); foreach(int num in set2) { if (set1.Contains(num)) { set1.Remove(num); set2.Remove(num); } } return new List<IList<int>>(){ set1.ToList(), set2.ToList()}; } ``` > [name=Jim][time=Wed, May 3, 2023] #### Java ```java= class Solution { public List<List<Integer>> findDifference(int[] nums1, int[] nums2) { Set<Integer> set1 = new HashSet<>(); Set<Integer> set2 = new HashSet<>(); for(Integer num : nums1) { set1.add(num); } for(Integer num : nums2) { set2.add(num); } Set<Integer> difference1 = new HashSet<>(set1); difference1.removeAll(set2); Set<Integer> difference2 = new HashSet<>(set2); difference2.removeAll(set1); List<List<Integer>> ans = new ArrayList<>(); ans.add(difference1.stream().toList()); ans.add(difference2.stream().toList()); return ans; } } ``` > 只能醜醜寫的我 > [name=Ron Chen][time=Wed, May 3, 2023] ```java= class Solution { public List<List<Integer>> findDifference(int[] nums1, int[] nums2) { Set<Integer> set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet()); // Delete nums2 element which in set1 and assign to set2 Set<Integer> set2 = Arrays.stream(nums2).filter(n -> !set1.contains(n)).boxed().collect(Collectors.toSet()); // Delete set1 element which in nums2 Arrays.stream(nums2).forEach(set1::remove); return Arrays.asList(new ArrayList<>(set1), new ArrayList<>(set2)); } } ``` > 解答區看到的 Java Stream API 用法,太猛了 ### Reference [回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)