The Intersection of Two Arrays II interview question asks you to find all the numbers that appear in both of two given arrays. Unlike the first version of this problem, you must include each element as many times as it appears in both arrays. For example, if '2' appears three times in the first array and twice in the second, '2' should appear twice in the final result.
This is a standard question at Google, Meta, and Amazon. It tests your ability to handle frequency management and choose the most appropriate data structure. It evaluation whether you understand the difference between a Set (unique elements) and a multiset or frequency map (tracking counts). It's a fundamental Hash Table interview pattern.
This problem is typically solved using a Frequency Map.
nums1 = [4, 9, 5], nums2 = [9, 4, 9, 8, 4]
nums1: {4: 1, 9: 1, 5: 1}.nums2:
[9]. Map: {4: 1, 9: 0, 5: 1}.[9, 4]. Map: {4: 0, 9: 0, 5: 1}.[9, 4].Always clarify the requirements: "Do I need unique elements or all duplicates?" If duplicates matter, think Hash Map with Counts. This is a core Array interview pattern for data reconciliation tasks.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Check If N and Its Double Exist | Easy | Solve | |
| Intersection of Two Arrays | Easy | Solve | |
| K-diff Pairs in an Array | Medium | Solve | |
| Minimum Common Value | Easy | Solve | |
| Count Pairs Whose Sum is Less than Target | Easy | Solve |