Intersection of Two Arrays II

EasyArrays & Hashingfrequency count

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given two integer arrays nums1 and nums2, return an array of their intersection. Unlike a plain set intersection, **each element in the result must appear as many times as it shows up in *both* arrays**. In other words, if a value occurs a times in nums1 and b times in nums2, it should appear exactly min(a, b) times in the output. The result may be returned in **any order**.

Examples

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
The value `2` appears twice in `nums1` and twice in `nums2`, so it appears `min(2,2) = 2` times. The value `1` is absent from `nums2`, so it is excluded.
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
`4` appears once in `nums1` and twice in `nums2`, giving `min(1,2) = 1`. `9` appears once in `nums1` and twice in `nums2`, giving `min(1,2) = 1`. `[9,4]` would also be accepted since order does not matter.
Input: nums1 = [1,2,3], nums2 = [4,5,6]
Output: []
There are no common values, so the intersection is empty.

Constraints

  • `1 <= nums1.length, nums2.length <= 1000`
  • `0 <= nums1[i], nums2[i] <= 1000`
  • The output may be returned in any order.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More frequency count problems