Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Tip/Trick

How to find dictionary duplicate values in C#

Rate me:
Please Sign up or sign in to vote.
4.05/5 (11 votes)
29 Dec 2013CPOL 70K   6   6
It is very simple and useful tip to find duplicate values from a dictionary.

Introduction

In this tip I will show you how to find dictionary duplicate values in C#. An expert you know this is very simple code, but nonetheless it is a very useful tip for many beginners in C#.

Background

Programmers like to create a dictionary for small data source to store key value type data. Keys are unique, but dictionary values may be duplicates.

Using the code  

Here I use a simple LINQ statement to find duplicate values from dictionary.

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"}    
};  
  
Response.Write("<b>dictionary elements........</b><br />");
        
//loop dictionary all elements   
foreach (KeyValuePair<int, string> pair in plants)  
{
    Response.Write(pair.Key + "....."+ pair.Value+"<br />");
}  
  
//find dictionary duplicate values.  
var duplicateValues = plants.GroupBy(x => x.Value).Where(x => x.Count() > 1);

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

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer http://asp-net-example.blogspot.com
Bangladesh Bangladesh
I am a web developer. I develop web application using asp.net c# and coldfusion. I am a tech blogger since 2007. I published more than 3000 c# asp.net and coldfusion tutorials, examples in my blogs. I also owner and founder of many social bookmarking sites. Last 10 years i also worked on search engine optimization.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Doug Domeny31-Dec-13 9:29
professionalDoug Domeny31-Dec-13 9:29 
QuestionSuggestion... Pin
Jason Vogel30-Dec-13 6:09
Jason Vogel30-Dec-13 6:09 
AnswerRe: Suggestion... Pin
cfindex30-Dec-13 6:28
cfindex30-Dec-13 6:28 
AnswerRe: Suggestion... Pin
johannesnestler9-Jan-14 3:56
johannesnestler9-Jan-14 3:56 
GeneralMy vote of 2 Pin
leiyangge29-Dec-13 14:22
leiyangge29-Dec-13 14:22 
GeneralRe: My vote of 2 Pin
Klaus Luedenscheidt29-Dec-13 18:20
Klaus Luedenscheidt29-Dec-13 18:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.