I've an array of integers as shown below:
2 2 4 3
I want to create a Lookup 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:
2 - 0,1
4 - 2
3 - 3
I tried something via LINQ which seems like a standard way of getting an instance of Lookup class but this is not compiling:
var prices = new int[] { 2,2,4,3};
var lookUp = prices.ToLookup<int,int>((x, i) => i + 1);
I don't understand why can I not simply instantiate the Lookup
class and add items into it like we do in Dictionary
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:
var prices = new int[] { 2,2,4,3};
var lookUp = new Lookup<int,int>();
for (int i = 0; i < prices.Length; i++)
lookUp.Add(prices[i], i);
This again doesn't compile as a note on MSDN says:
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.
Can someone help me achieving my key objective? I'm struggling a bit to wrap my head around how Lookup
implementation has been made available in C#.