Check whether two integer arrays are equal as multisets

Quick Overview

Decide whether two integer arrays hold the same multiset, meaning the same values with the same number of occurrences in any order. It tests frequency counting with a hash map, early exits, and the difference between set equality and multiset equality.

Check whether two integer arrays are equal as multisets

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two integer arrays `nums1` and `nums2` representing multisets, determine if they are equal. Two multisets are considered equal if they contain the same elements with the same frequencies. The arrays may contain duplicates, and the solution must handle them efficiently. Return `True` if the two multisets are equal, and `False` otherwise. ### Function Signature ```python def are_multisets_equal(nums1: list[int], nums2: list[int]) -> bool: ``` ### Rules - The order of elements inside each array does not matter. - Every value counts as many times as it appears. A value that appears three times in one array and twice in the other makes the multisets different. - The two arrays may have different lengths. ### Constraints - `1 <= len(nums1), len(nums2) <= 10^5` - `-10^9 <= nums1[i], nums2[i] <= 10^9` ### Examples ```text Input: nums1 = [1,2,3,2], nums2 = [2,1,2,3] Output: True ``` Both arrays contain 1 once, 2 twice and 3 once, so they are equal multisets. ```text Input: nums1 = [1,2,2], nums2 = [1,1,2] Output: False ``` `nums1` has 1 once and 2 twice, while `nums2` has 1 twice and 2 once. The frequencies differ. ```text Input: nums1 = [4,4,4], nums2 = [4,4] Output: False ``` Both arrays hold only the value 4, but it appears three times in `nums1` and twice in `nums2`.

Overview: Decide whether two integer arrays hold the same multiset, meaning the same values with the same number of occurrences in any order. It tests frequency counting with a hash map, early exits, and the difference between set equality and multiset equality.

Pinterest logo
Pinterest
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two integer arrays nums1 and nums2 representing multisets, determine if they are equal.

Two multisets are considered equal if they contain the same elements with the same frequencies. The arrays may contain duplicates, and the solution must handle them efficiently.

Return True if the two multisets are equal, and False otherwise.

Function Signature

def are_multisets_equal(nums1: list[int], nums2: list[int]) -> bool:

Rules

  • The order of elements inside each array does not matter.
  • Every value counts as many times as it appears. A value that appears three times in one array and twice in the other makes the multisets different.
  • The two arrays may have different lengths.

Constraints

  • 1 <= len(nums1), len(nums2) <= 10^5
  • -10^9 <= nums1[i], nums2[i] <= 10^9

Examples

Input: nums1 = [1,2,3,2], nums2 = [2,1,2,3]
Output: True

Both arrays contain 1 once, 2 twice and 3 once, so they are equal multisets.

Input: nums1 = [1,2,2], nums2 = [1,1,2]
Output: False

nums1 has 1 once and 2 twice, while nums2 has 1 twice and 2 once. The frequencies differ.

Input: nums1 = [4,4,4], nums2 = [4,4]
Output: False

Both arrays hold only the value 4, but it appears three times in nums1 and twice in nums2.