title: Get all combinations of shared elements in a list
body: <p>For a list:</p>
  
  <pre><code>terms &lt;- list(Item1 = c("a", "b", "c", "d"),
                Item2 = c("a", "e", "f", "g"),
                Item3 = c("b", "e", "h", "i"),
                Item4 = c("j", "k"))
  </code></pre>
  
  <p>I would like to get the number of shared letters between each pair of items in the list. The expected output is therefore:</p>
  
  <pre><code>     [,1] [,2] [,3] [,4]
  [1,]    4    1    1    0
  [2,]    1    4    1    0
  [3,]    1    1    4    0
  [4,]    0    0    0    2
  </code></pre>
  
  <p>From a previous StackOverflow answer, I found one possible solution:</p>
  
  <pre><code>overlapLength &lt;- function(x, y) mapply(function(x, y) 
    length(intersect(x, y)), terms[x], terms[y])
  s &lt;- seq_along(terms)
  outer(s, s, overlapLength)
  </code></pre>
  
  <p>But this is very slow for my list, which is very large (~9,000 items).</p>
  
  <p>Is there a faster way to do this?</p>
  
  <hr>
  
  <p>Thanks everyone for your input. I timed all answers with the first 100 items of my list.</p>
  
  <pre><code>&gt; system.time(f_crossprod(go))
     user  system elapsed 
    0.024   0.001   0.025 
  &gt; system.time(f_crossprod2(go))
     user  system elapsed 
    0.007   0.000   0.008 
  &gt; system.time(f_mapply(go))
     user  system elapsed 
    2.018   0.032   2.059 
  &gt; system.time(f_outer(go))
     user  system elapsed 
    1.950   0.016   1.979 
  &gt; system.time(f_combn(go))
     user  system elapsed 
    1.056   0.005   1.062 
  &gt; system.time(f_Rcpp(go))
     user  system elapsed 
  163.236  84.226 249.240 
  </code></pre>
  
  <p>I then timed the <code>outer</code> and <code>Matrix::crossprod</code> solutions with the entire list of ~9,000 elements. The <code>outer</code> solution ran in about 55 minutes. The <code>Matrix::crossprod</code> solution ran in about 0.1 seconds!</p>
  
  <p>It is possible I have made an error in implementation of the Rcpp function. However, @alexis_laz if you make your comment an answer I will accept it.</p>
  
  <p>By the way, sorry I was not clear, I am not interested in the values on the diagonal.</p>