title: Time efficient Partial Inverted Index building
body: <p>I need to build a partial <a href="http://en.wikipedia.org/wiki/Inverted_index" rel="noreferrer"><code>Inverted Index</code></a>. Something like:  </p>
  
  <pre><code>l = {{x, {h, a, b, c}}, {y, {c, d, e}}}
  iI[l]
  (*
  -&gt; {{a, {x}}, {b, {x}}, {c, {x, y}}, {d, {y}}, {e, {y}}, {h, {x}}}
  *)
  </code></pre>
  
  <p>I think it is pretty clear what it does. In the input list, the {x, y ...} are unique, while the {a, b, c, ..} are not.  The output ought to be ordered by <code>#[[1]]</code>.</p>
  
  <p>Right now, I am doing this:  </p>
  
  <pre><code>iI[list_List] := {#, list[[Position[list, #][[All, 1]]]][[All, 1]]} &amp; /@ 
                       (Union@Flatten@Last@Transpose@list)
  </code></pre>
  
  <p>But it looks too convoluted for such an easy task, seems too slow, and I should be able to cope with Legion.</p>
  
  <p>A test drive to compare your results:  </p>
  
  <pre><code>words = DictionaryLookup[];
  abWords = DictionaryLookup["ab" ~~ ___];
  l = {#, RandomChoice[abWords, RandomInteger[{1, 30}]]} &amp; /@ words[[1 ;; 3000]];
  First@Timing@iI[l]
  (*
  -&gt; 5.312
  *)
  </code></pre>
  
  <p>So, any ideas for an speedup?</p>