I would like to use linq to group the data using value and to return the corresponding indexes as array.
Example
int[] input = {0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,2,2,2,2}
Expected output
Dictionary<int,int[]> ouput = {0->[0,1,2,3,8,9,10,11]; 1 -> [4,5,6,7,12,13,14,15]; 2 -> [16,17,18,19]}
Can anybody guide me?
You can use this:
This first selects an anonymous type to save the original index in the array, then groups by the value and afterwards transforms the grouped result into a dictionary with the key of each group as the key of the dictionary and from all elements in the corresponding group the index is selected.
A shorter way would be this:
This would result in a
Lookup<int, int>which is semantically the same as theDictionary<int, int[]>.