We have the following dictionary...
Dictionary <int,string> dCits = new Dictionary<int, string>();
dCits.Add(1,"London");
dCits.Add(2, "London");
dCits.Add(3, "Paris");
dCits.Add(4, "Berlin");
dCits.Add(5, "Paris");
dCits.Add(6, "Madrid");
dCits.Add(7, "Berlin");
dCits.Add(8, "Madrid");
dCits.Add(9, "Madrid");
dCits.Add(10, "Atenas");
dCits.Add(16, "London");
dCits.Add(62, "Paris");
dCits.Add(63, "Roma");
the idea is to take the cities that are only repeated N times if N is 2 London will be taken one and up to 2 times, or if N is 3 take the three london or 3 madrid including those of 2 and 1. if only 1 is required I mean N=1 would be like a Distinct... (as I indicate in the code below) the problem is when there are more than 1 that the distinct does not look good
foreach(var value in dCits.Values.Distinct())
{
// aca indicar cuantas veces se necesitan dependiendo de N.
}
any ideas, be it with linq, a select or another command to solve this?
UPDATE The result of the new dictionary would be something like this:
if N=2
1,"London"
2, "London"
3, "Paris"
4, "Berlin"
5, "Paris"
6, "Madrid"
7, "Berlin"
8, "Madrid"
10, "Atenas"
63, "Roma"
if only at least 2 repeated are required and also selecting those that have one. for example rome
We group by the values, and apply it to a new dictionary that has the city as key and the value count as value... and then with a where, we filter the list
and you would have the keys that you want in new
You can use
GroupBy
to group the values and applyany
for only those that are repeated greater than 1.Example: