//initialize a dictionary with keys and values.
Dictionary plants = new Dictionary() {
{1,"Speckled Alder"},
{2,"Apple of Sodom"},
{3,"Hairy Bittercress"},
{4,"Pennsylvania Blackberry"},
{5,"Apple of Sodom"},
{6,"Water Birch"},
{7,"Meadow Cabbage"},
{8,"Water Birch"}
};
Response.Write("dictionary elements........ ");
//loop dictionary all elements
foreach (KeyValuePair pair in plants)
{
Response.Write(pair.Key + "....."+ pair.Value+" ");
}
//find dictionary duplicate values.
var duplicateValues = plants.GroupBy(x => x.Value).Where(x => x.Count() > 1);
Response.Write(" dictionary duplicate values.......... ");
//loop dictionary duplicate values only
foreach(var item in duplicateValues)
{
Response.Write(item.Key+" ");
}
|