title: How to turn this solution from O(n^2) to O(n)?
body: <p>I am having some problems turning this solution from O(n^2) to O(n). Can anyone kindly help? I am not able to think of any ways to make the time complexity O(n).</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//MERGE SORTED ARRAY
  /*arr1 = [0,3,4,31]
  arr2 = [4,6,30]*/
   
  const mergeSortedArrays = (arr1, arr2) =&gt; {
    let arr = [];
    let flag = true;
   
    // MERGING ARRAYS
    for (let i = 0; i &lt; arr1.length; i++) {
      arr.push(arr1[i]);//PUSHING ARRAY1 in arr
    }
    for (let i = 0; i &lt; arr2.length; i++) {
      arr.push(arr2[i]);//PUSING ARRAY2 in arr
    }
   
    //SORTING ARRAYS
    while (flag) {
      for (let i = 0; i &lt; arr.length; i++) {
        if (arr[i] &gt; arr[i + 1]) {
          let temp = arr[i + 1];
          arr[i + 1] = arr[i];
          arr[i] = temp;
          flag = true;
        } else {
          flag = false;
        }
      }
    }
   
    console.log(arr1);
    console.log(arr2);
    console.log(arr);//FINAL MERGED &amp; SORTED ARRAY
    // return arr;
    
  }
   
  mergeSortedArrays([0, 3, 4, 31], [4, 6, 30]);</code></pre>
  </div>
  </div>
  </p>