title: isSubset of two arrays
body: <p>I want to get the subset of two arrays with a few test cases. I do not want to use the javascript .every function as that makes it too easy. My attempt works but I don't think it's optimal for time and space complexity O(n^2).</p>
  
  <p>My test cases are as follows:</p>
  
  <p>1) All values of <code>array1</code> should be defined in <code>array2</code></p>
  
  <p>2) If duplicate values exist in <code>array1</code>, they should also take into account in <code>array2</code>. for example, if <code>arr1 = ["a", "a"]</code> and <code>arr2 = ["b", "a"]</code> then isSubset is <code>false</code> because <code>"a"</code> appears twice in the first but only once in the second.</p>
  
  <p>Here is my solution:</p>
  
  <pre><code>let arr1 = ["A", "B", "C"];
  let arr2 = ["C", "A", "B", "D"];
  
  function isSubset(a, b) {
    if(a.length &gt; b.length) {
      return false;
    }
  
    var myHash = {};
    for (var i = 0; i &lt; a.length; ++i) {
      if(myHash[a[i]]) {
        myHash[a[i]] += 1;
      } else {
        myHash[a[i]] = 1;
      }
    }
  
    var myHash2 = {};
    for (var i = 0; i &lt; b.length; ++i) {
      if(myHash2[b[i]]) {
        myHash2[b[i]] += 1;
      } else {
        myHash2[b[i]] = 1;
      }
    }
  
  
    for(var i in myHash) {
      for(var j in myHash2) {
        if(i == j &amp;&amp; myHash[i] &gt; myHash2[j]) {
          return false;
        }
      }
    }
  
  
    return true;
  }
  
  // test
  var a = ["B", "A", "C", "A"];
  var b = ["A", "B", "C", "D"];
  var c = isSubset(a, b);
  console.log(c);
  </code></pre>
  
  <p>Codepen: <a href="https://codepen.io/anon/pen/KYPayp?editors=0010" rel="nofollow noreferrer">https://codepen.io/anon/pen/KYPayp?editors=0010</a></p>