Intersection of Two Arrays II
The problem requires returning the intersection of two integer arrays nums1
and nums2
, where each element in the result must appear as many times as it shows in both arrays. You can use a dictionary to keep track of the counts of elements in one array and then use that information to find the intersection.
Here’s a stepbystep guide to solving the problem:
Count Elements in
nums1
: Use a dictionary to count how many times each element appears innums1
.Find the Intersection: Iterate through
nums2
, and for each element, if it appears in the dictionary (and the count is greater than 0), add it to the result and decrease the count in the dictionary.Return the Result: The result is the list of elements that appear in both
nums1
andnums2
.
Here’s the code:


Explanation:
 The dictionary
counts
is used to keep track of how many times each number appears innums1
.  When iterating through
nums2
, if a number appears incounts
(with a count greater than 0), it is part of the intersection, and you add it to the result and decrease the count in the dictionary.  The result list will include each intersecting number as many times as it appears in both arrays.
This approach ensures that the result reflects the actual intersection, including the number of occurrences, and meets the problem’s constraints.
Identifying Problem Isomorphism
“Intersection of Two Arrays” is isomorphic to “Intersection of Two Arrays II” (#350). Both problems involve finding the intersection of two arrays, meaning the elements that the two arrays have in common.
In “Intersection of Two Arrays”, the task is to find the distinct elements that both arrays have. In contrast, “Intersection of Two Arrays II” requires you to find all elements that both arrays have, including duplicates. In this sense, the latter problem can be seen as a generalized version of the former.
This is not an exact mapping. The major difference lies in handling duplicates. The first only counts distinct elements while the second also counts duplicates. Therefore, while the problems share a core concept and similar logic, the solutions might have subtle differences to accommodate these specific requirements.


Problem Classification
This problem is a classical Array Manipulation problem with aspects of Frequency Counting. It requires elements from two arrays to be compared, counted, and collected based on the specific rules given, which makes it a subcategory of the more general Intersection of Two Arrays problems. It specifically involves concepts such as:
 Array Traversal: You need to traverse both arrays.
 Frequency Counting: You need to count the frequency of each element’s occurrence in both arrays.
 Intersection Calculation: You need to determine which elements appear in both arrays.
 Multiple Occurrence Handling: You must account for multiple occurrences of an element in the intersection array.
Language Agnostic Coding Drills
Array Sorting: It’s important to know how to sort an array. In many languages, there’s a builtin function for this. The sorting is useful here to ensure that identical numbers from both arrays follow each other as we traverse the sorted arrays, making it easier to find common elements.
Array Traversal with Two Pointers: This concept involves traversing two sorted arrays simultaneously with two separate indices (i and j in the provided code). The logic is such that if the element in the first array is smaller, we move the pointer in the first array forward. If the element in the second array is smaller, we move the pointer in the second array forward. If they’re equal, we know we’ve found a common element.
Array Appending: Knowing how to add elements to an array is essential. Here, we’re adding common elements to an output array.
Conditional Statements and Loops: The solution uses conditional statements (
if
andelif
) inside awhile
loop to compare elements and increment pointers. Knowing how to construct these is fundamental to most programming tasks.
To solve the problem:
 Start by sorting both arrays. This makes it easier to find matching elements because they will appear in the same order in both arrays.
 Initialize two pointers (i and j) at the start of both sorted arrays.
 Use a
while
loop to go through the sorted arrays. This loop should continue until one of the pointers reaches the end of its array.  In each iteration of the loop, compare the elements at the current indices of the sorted arrays.
 If they are equal, add them to the result array and move both pointers forward.
 If the element at the first pointer is less than the element at the second pointer, move the first pointer forward. This is because the first element is smaller and there’s no chance of finding this element in the second array as the second array is sorted too.
 If the element at the second pointer is less than the element at the first pointer, move the second pointer forward.
 At the end of the loop, return the result array, which will contain the intersection of the two input arrays.
Targeted Drills in Python
 Array Sorting:


 Array Traversal with Two Pointers:


 Array Appending:


 Conditional Statements and Loops:


Now, to combine all these into an integrated drill, we can use the following code:

