Array Is Contained in Other Array

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to check if one array is contained in another array in JavaScript. We will use various array methods such as for...of, Set, some(), and filter() to write a function that can determine if the elements of the first array are present in the second array, regardless of their order. This lab will help you understand how to manipulate arrays in JavaScript and write efficient code to solve common programming problems.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/loops("`Loops`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") subgraph Lab Skills javascript/variables -.-> lab-28149{{"`Array Is Contained in Other Array`"}} javascript/data_types -.-> lab-28149{{"`Array Is Contained in Other Array`"}} javascript/arith_ops -.-> lab-28149{{"`Array Is Contained in Other Array`"}} javascript/comp_ops -.-> lab-28149{{"`Array Is Contained in Other Array`"}} javascript/cond_stmts -.-> lab-28149{{"`Array Is Contained in Other Array`"}} javascript/loops -.-> lab-28149{{"`Array Is Contained in Other Array`"}} javascript/higher_funcs -.-> lab-28149{{"`Array Is Contained in Other Array`"}} end

Function to Check if an Array is Contained in Another Array

To begin coding, open the Terminal/SSH and type node. This function checks whether all the elements of the first array are present in the second array, irrespective of their order.

Here are the steps to follow:

  1. Use a for...of loop to iterate over a Set created from the first array.
  2. Apply Array.prototype.some() to verify if all distinct values are present in the second array.
  3. Use Array.prototype.filter() to compare the number of occurrences of each distinct value in both arrays.
  4. If the count of any element is greater in the first array than the second one, return false. If not, return true.

Check out the code below to see how it works:

const isContainedIn = (a, b) => {
  for (const v of new Set(a)) {
    if (
      !b.some((e) => e === v) ||
      a.filter((e) => e === v).length > b.filter((e) => e === v).length
    )
      return false;
  }
  return true;
};

To test the function, use the following code:

isContainedIn([1, 4], [2, 4, 1]); // true

Summary

Congratulations! You have completed the Array Is Contained in Other Array lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like