SoFunction
Updated on 2025-03-08

Methods to find duplicate values ​​in Dictionary in C#


//initialize a dictionary with keys and values.   
Dictionary<int, string> plants = new Dictionary<int, string>() {   
    {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"}   
}; 

("<b>dictionary elements........ </b><br />");

//loop dictionary all elements  
foreach (KeyValuePair<int, string> pair in plants) 
{
    ( + "....."+ +"<br />");


//find dictionary duplicate values. 
var duplicateValues = (x => ).Where(x => () > 1);

("<br /><b>dictionary duplicate values..........</b><br />");

//loop dictionary duplicate values only           
foreach(var item in duplicateValues) 
{
    (+"<br />");
}