title: How to convert an array to a look up data structure
body: <p>I've an array of integers as shown below:</p>
  
  <pre><code>2 2 4 3
  </code></pre>
  
  <p>I want to create a <a href="https://stackoverflow.com/q/1403493/465053">Lookup</a> for above data. I want that for each unique number present in the array, I can maintain the indexes as a linked list in case it gets repeated:</p>
  
  <pre><code>2 - 0,1
  4 - 2
  3 - 3
  </code></pre>
  
  <p>I tried something via LINQ which seems like a standard way of getting an instance of Lookup class but this is not compiling:</p>
  
  <pre><code>var prices = new int[] { 2,2,4,3};
  var lookUp = prices.ToLookup&lt;int,int&gt;((x, i) =&gt; i + 1);
  </code></pre>
  
  <p>I don't understand why can I not simply instantiate the <code>Lookup</code> class and add items into it like we do in <code>Dictionary</code> class. Whenever it finds the same key being added into it again, it should simply create a collection instead. Sample code that I was speculating to work:</p>
  
  <pre><code>var prices = new int[] { 2,2,4,3};
  var lookUp = new Lookup&lt;int,int&gt;();        
  for (int i = 0; i &lt; prices.Length; i++)
      lookUp.Add(prices[i], i);
  </code></pre>
  
  <p>This again doesn't compile as <a href="https://msdn.microsoft.com/en-us/library/bb460184%28v=vs.110%29.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow noreferrer">a note on MSDN</a> says:</p>
  
  <blockquote>
    <p>There is no public constructor to create a new instance of a
    Lookup. Additionally, Lookup objects
    are immutable, that is, you cannot add or remove elements or keys from
    a Lookup object after it has been created.</p>
  </blockquote>
  
  <p>Can someone help me achieving my key objective? I'm struggling a bit to wrap my head around how <code>Lookup</code> implementation has been made available in C#.</p>