title: Find common parent-path in list of files and directories
body: <p>I got a list of files and directories <code>List&lt;string&gt; pathes</code>. Now I'd like to calculate the deepest common branch every path is sharing with each other.</p>
  
  <p>We can assume that they all share a common path, but this is unknown in the beginning.</p>
  
  <p>Let's say I have the following three entries:</p>
  
  <ul>
  <li>C:/Hello/World/This/Is/An/Example/Bla.cs</li>
  <li>C:/Hello/World/This/Is/Not/An/Example/</li>
  <li>C:/Hello/Earth/Bla/Bla/Bla</li>
  </ul>
  
  <p>This should get the result: C:/Hello/ as Earth is breaking this "chain" of subdirectories.</p>
  
  <p>Second example:</p>
  
  <ul>
  <li>C:/Hello/World/This/Is/An/Example/Bla.cs</li>
  <li>C:/Hello/World/This/Is/Not/An/Example/</li>
  </ul>
  
  <p>-> C:/Hello/World/This/Is/</p>
  
  <p>How would you proceed? I tried to use string.split(@"/") and start with the first string and check if every part of this array is contained in the other strings. However, this would be a very expensive call as I'm iterating (list_of_entries)^list_of_entries. Is there any better solution available?</p>
  
  <p>My current attempt would be something like the following (C# + LINQ):</p>
  
  <pre><code>    public string CalculateCommonPath(IEnumerable&lt;string&gt; paths)
      {
          int minSlash = int.MaxValue;
          string minPath = null;
          foreach (var path in paths)
          {
              int splits = path.Split('\\').Count();
              if (minSlash &gt; splits)
              {
                  minSlash = splits;
                  minPath = path;
              }
          }
  
          if (minPath != null)
          {
              string[] splits = minPath.Split('\\');
              for (int i = 0; i &lt; minSlash; i++)
              {
                  if (paths.Any(x =&gt; !x.StartsWith(splits[i])))
                  {
                      return i &gt;= 0 ? splits.Take(i).ToString() : "";
                  }
              }
          }
          return minPath;
      }
  </code></pre>