BackeasyHashingCognizant

Common Element Frequencies Solution

Problem Statement

Given two arrays of integers arr1 and arr2, find the common elements between them, preserving the minimum frequency of each common element. Return the resulting array of common elements.

Example 1
Input
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5], [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: with input [1, 1, 2, 2, 3, 3, 4, 4, 5, 5] and [1, 1, 2, 2, 3, 3, 4, 4, 5, 5], we find the common elements between them, preserving the minimum frequency of each common element. Since both arrays have the same elements with the same frequency, the resulting array will be the same as the input arrays.

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

Explanation: Step-by-step: with input [1, 2, 2, 3, 3, 3] and [2, 2, 3, 3, 4, 4], we find the common elements between them, preserving the minimum frequency of each common element. The common elements are 2 and 3. The minimum frequency of 2 is 2 and the minimum frequency of 3 is 2. Therefore, the resulting array will be [2, 2, 3, 3].

Constraints

  • 1 <= arr1.length, arr2.length <= 1000
  • 0 <= arr[i] <= 1000
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Common Element Frequencies — Problem Statement & Solution Guide

HashingEasyFrequency Map
TimeO(n+m)
|
SpaceO(min(n,m))

Problem Description

Given two arrays of integers arr1 and arr2, find the common elements between them, preserving the minimum frequency of each common element. Return the resulting array of common elements.

Examples

Example 1

Input

[1, 1, 2, 2, 3, 3, 4, 4, 5, 5], [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: with input [1, 1, 2, 2, 3, 3, 4, 4, 5, 5] and [1, 1, 2, 2, 3, 3, 4, 4, 5, 5], we find the common elements between them, preserving the minimum frequency of each common element. Since both arrays have the same elements with the same frequency, the resulting array will be the same as the input arrays.

Example 2

Input

[1, 2, 2, 3, 3, 3], [2, 2, 3, 3, 4, 4]

Output

[2, 2, 3, 3]

Explanation: Step-by-step: with input [1, 2, 2, 3, 3, 3] and [2, 2, 3, 3, 4, 4], we find the common elements between them, preserving the minimum frequency of each common element. The common elements are 2 and 3. The minimum frequency of 2 is 2 and the minimum frequency of 3 is 2. Therefore, the resulting array will be [2, 2, 3, 3].

Constraints

  • 1 <= arr1.length, arr2.length <= 1000
  • 0 <= arr[i] <= 1000

Optimal Approach & Strategy

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)).

Brute Force Approach

For each element in arr1, search and remove from arr2. Time O(N*M).

Verified Code Solutions

JavaScript Solution
Time: O(n+m)
function solution(arr1, arr2) {
      const map1 = {};
      const map2 = {};
      const result = [];

      for (let num of arr1) {
         if (map1[num]) {
            map1[num]++;
         } else {
            map1[num] = 1;
         }
      }

      for (let num of arr2) {
         if (map2[num]) {
            map2[num]++;
         } else {
            map2[num] = 1;
         }
      }

      for (let num in map1) {
         if (map2[num]) {
            const minCount = Math.min(map1[num], map2[num]);
            for (let i = 0; i < minCount; i++) {
               result.push(parseInt(num));
            }
         }
      }

      return result;
   }

Asked in Top Tech Interviews

Cognizant

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.