If you really need an array of integers the you have to iterate two times on the
Dictionary
.
e.g. (
rd
is the
output Dictionary
)
static void Main(string[] args)
{
string s1 = "s1";
string s2 = "s2";
string s3 = "s3";
string[] a1 = { s1, s2, s3 };
string[] a2 = { s1, s2 };
Dictionary <int, string[]> d = new Dictionary<int, string[]>();
d.Add(1, a1);
d.Add(2, a2);
Dictionary <string,int > cd = new Dictionary<string, int>();
Dictionary <string, int> cdi = new Dictionary<string, int>();
foreach (int k in d.Keys)
{
foreach (string s in d[k])
{
if (cd.ContainsKey(s))
cd[s]++;
else
cd.Add(s, 1);
}
}
Dictionary<string, int[]> rd = new Dictionary<string, int[]>();
foreach (string s in cd.Keys)
{
rd.Add(s, new int[cd[s]]);
cdi.Add(s, 0);
}
foreach (int k in d.Keys)
{
foreach (string s in d[k])
{
rd[s][cdi[s]++] = k;
}
}
}
[Update]
I don't get your new requirements completely (I cannot see proper declaration of your generic types), but I suppose you may modify the code I provided to fit your needs (it shouldn't be hard).
[/Update]