Array Subset

Learn how to tell if two arrays contain the same values. We'll discuss how to deal with both primitive values and objects and we'll go over how a JavaScript Map helps us with this problem.

Array Subset

Instructions

Write a function that accepts two parameters, both arrays. The arrays can have both strings and numbers. Return true if the second array is a subset of the first.

In other words, determine if every item in the 2nd array is also present somewhere in the 1st array.

Input: Array of Numbers & Strings, Array of Numbers & Strings

Output: Boolean

Examples

arraySubset([2, 1, 3], [1, 2, 3]); // -> true
arraySubset([2, 1, 1, 3], [1, 2, 3]); // -> true
arraySubset([1, 2], [1, 2, 3]); // -> false
arraySubset([1, 2, 3], [1, 2, 2, 3]); // -> false

Hints

  • This problem has multiple solutions with different time complexities.
  • We’ll need to consider how to deal with repeats, such as when an item is present twice.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.