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] <= 1000Problem Overview: You are given two integer arrays nums1 and nums2. The task is to return two lists: elements that appear in nums1 but not in nums2, and elements that appear in nums2 but not in nums1. Each result must contain distinct values only.
This is essentially a set-difference problem. You compare the unique elements of both arrays and determine which values exist exclusively in one array. The challenge is avoiding duplicates while keeping the algorithm efficient.
Approach 1: Using Sorting to Solve the Problem (O(n log n) time, O(1) extra space)
This approach sorts both arrays first, then scans them to identify elements that appear in only one array. After sorting, you iterate through both arrays with two pointers. When values match, move both pointers. When one value is smaller, it means that value does not appear in the other array at that position, so it belongs in the result. Duplicate values are skipped during traversal to ensure the output only contains unique elements.
The main advantage is minimal extra memory usage since sorting allows comparison without additional hash structures. The tradeoff is the O(n log n) sorting cost. This approach is useful when memory is constrained or when the arrays are already sorted. Sorting-based comparisons are common patterns in array problems where duplicates must be handled carefully.
Approach 2: Using a HashMap for Efficient Lookup (O(n + m) time, O(n + m) space)
The optimal solution converts both arrays into hash sets. A set automatically removes duplicates and provides constant-time membership checks. First insert all elements from nums1 into a set and all elements from nums2 into another set. Then iterate through the first set and add elements not found in the second set to the result list. Repeat the same process in reverse for the second set.
The key insight is that hash lookups run in average O(1) time, so the comparison becomes linear overall. Building the sets costs O(n + m), and the difference checks also run in linear time. This approach is a classic application of hash tables for fast membership testing and is commonly used in problems involving deduplication and set operations on arrays.
Recommended for interviews: The hash-set approach is what interviewers typically expect. It demonstrates that you recognize the problem as a set-difference operation and can use constant-time lookups to reduce complexity to O(n + m). Discussing the sorting approach first can show baseline reasoning, but the hash-based solution signals stronger familiarity with common array + hash table optimization patterns.
This approach involves first sorting the data to simplify the problem, allowing for efficient searching or manipulation afterwards. Sorting can often reduce the complexity of further operations by providing a clear ordering of elements.
Depending on the problem's specifics, sorting may allow for easier handling of duplicates or simplification of conditions. Note that the initial overhead of sorting is compensated by the reduced complexity of the subsequent operations.
This C code sorts the array using the quicksort algorithm available through the standard library's qsort function. The compare function helps define the sorting order.
Time Complexity: O(n log n) due to the sorting operation.
Space Complexity: O(1) if in-place sorting is used.
In this approach, we utilize a HashMap (or a dictionary in languages like Python) to keep track of elements and perform efficient lookups. This is particularly useful when the problem requires checking for existence of elements or handling duplicates.
This approach reduces the time complexity of these operations to O(1) on average, which is significantly faster than scanning through an array.
This C code uses a simple hash table to track occurrences of elements in the array, allowing for O(1) average time complexity for lookups and insertions.
Time Complexity: O(n) for iterating through the array.
Space Complexity: O(U), where U is the universe of possible values.
| Approach | Complexity |
|---|---|
| Using Sorting to Solve the Problem | Time Complexity: O(n log n) due to the sorting operation. |
| Using a HashMap for Efficient Lookup | Time Complexity: O(n) for iterating through the array. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting + Two Pointers | O(n log n + m log m) | O(1) extra (excluding output) | When memory is limited or arrays are already sorted |
| Hash Set / HashMap Lookup | O(n + m) | O(n + m) | General case and optimal interview solution with fast lookups |
Find the Difference of Two Arrays - Leetcode 2215 - Python • NeetCodeIO • 18,329 views views
Watch 9 more video solutions →Practice Find the Difference of Two Arrays with our built-in code editor and test cases.
Practice on FleetCode