BackeasyHashing Swiggy

Intersection of Two Arrays II Solution

Problem Statement

Given two supply chains, find the common product codes between the two deliveries, preserving the delivery count.

Example 1
Input
[1,2,2,1,1,3,4,4,5,5,5,5]
Output
[2,2,1,1]

Explanation: Step-by-step: Given two arrays, we first create a hashmap to store the frequency of each element in the first array. Then, we iterate over the second array and for each element, we add it to the output list as many times as its count in the hashmap. In this example, the output is [2,2,1,1] because the frequency of 2 and 1 in the hashmap is 2 and 2 respectively, and the frequency of 2 and 1 in the second array is 2 and 2 respectively.

Example 2
Input
[1,1,2,2,3,3,4,4,5,5]
Output
[1,1,2,2,3,3,4,4,5,5]

Explanation: Step-by-step: Given two arrays, we first create a hashmap to store the frequency of each element in the first array. Then, we iterate over the second array and for each element, we add it to the output list as many times as its count in the hashmap. In this example, the output is [1,1,2,2,3,3,4,4,5,5] because the frequency of each element in the hashmap is equal to its frequency in the second array.

Constraints

  • 1 <= arr1.length, arr2.length <= 1000
  • 0 <= arr[i] <= 1000
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free