easyHashingPattern: Frequency Map
Common Element Frequencies Solution
Problem Statement
Given two arrays of integers `arr1` and `arr2`, find the common elements between them, preserving the frequency of each common element. Return the resulting array of common elements and their frequencies.
Examples
Example 1:
Input:[1,2,2,1] and [2,2]
Output:[2,2]
Explanation: The common product codes between the two deliveries are 2, with a count of 2
Constraints
- 1 <= arr1.length, arr2.length <= 1000
- 0 <= arr[i] <= 1000
Time: O(N+M) Space: O(min(N,M))
Create frequency map of arr1. Iterate arr2: if element in map and freq > 0, add to result and decrement freq. Time O(N+M), Space O(min(N, M)).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
