title: Looping through string array, find the two strings with the most overlap and merge them. REPEAT until one string left
body: <p>I have a string array named 'collection' as shown below. I want to loop through the array and find the strings with the most overlap - that is the most same characters in a row. Once I find the two strings I want to append a new string to the array with the concatenation of these two strings and then remove the original strings. The catch is that the concatenation must ignore common characters. For example: 'hello wo' &amp; 'o world' will become 'hello world'. I want to repeat this process until one string is left. </p>
  
  <pre><code>let collection = ["all is well", "ell that en", "hat end", "t ends well"];
  let longestOverlap = 0;
  let longestOverlapChars = '';
  
  for (i = 0 ; i &lt; collection.length ; i++){
       for (j= i+1 ; j &lt; collection.length ; j++){
  
          findOverlap(collection[i],collection[j]); 
       }
  }
  
  function findOverlap(a, b, originalb) {
  
    if (a.indexOf(b) &gt;= 0) {
      if (longestOverlapChars.length &lt; b.length){      
        longestOverlapChars = b;
        longestOverlap = longestOverlapChars.length;
        console.log(longestOverlapChars, longestOverlap);
      }
  
  
      return console.log(a &amp;&amp; originalb) ;
    }
  
    return findOverlap(a, b.substring(0, b.length - 1));
  }
  </code></pre>
  
  <p>My results show the 4 outcomes in the console:
  ell, hat en, t en, t end.</p>
  
  <p>This shows the instances of overlapping text.</p>
  
  <p>Subsequently 'hat en' is the largest so I want to combine the two strings that have this overlap. So my new string array will look like this:[" all is well", "ell that end", "t ends well"];</p>
  
  <p>There is now 3 strings and I want to repeat this process until I have ["all is well that ends well'].</p>
  
  <p>Can anyone give me some direction with my code? Also, any coding tips will be helpful. Trying to improve.</p>