2dbi

Check if one array contains all elements of another

viaLeetCode

Problem Given two arrays, determine whether the second contains ALL elements of the first (clarify: as a set, or respecting multiplicities?).

Input / Output

  • Input: arrays a and b.
  • Output: boolean — every element of a present in b.

Constraints

  • Sizes up to 10^5; O(m + n) expected via hashing.

Example

  • a = [1,2,2], b = [2,1,3] → true as sets; false with multiplicity (b has one 2).

Expected approach

  • Set semantics: build a hash set of b, verify each a element — O(m+n). Multiset semantics: frequency map of b, decrement per a element, fail on underflow. Sorting both + merge-walk is the O(n log n) no-hash alternative. The main evaluation is asking the multiplicity/duplicates question before coding and handling empty arrays cleanly.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account