Click here to Skip to main content
Click here to Skip to main content

How to find dictionary duplicate values in C#

, 29 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

//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)

Share

About the Author

Sk. Saiful Alam
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.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 4 PinprofessionalDoug Domeny31-Dec-13 10:29 
QuestionSuggestion... PinmemberJason Vogel30-Dec-13 7:09 
AnswerRe: Suggestion... PinmemberSk. Saiful Alam30-Dec-13 7:28 
AnswerRe: Suggestion... [modified] Pinmemberjohannesnestler9-Jan-14 4:56 
GeneralMy vote of 2 Pinmemberleiyangge29-Dec-13 15:22 
GeneralRe: My vote of 2 PinmemberKlaus Luedenscheidt29-Dec-13 19:20 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 29 Dec 2013
Article Copyright 2013 by Sk. Saiful Alam
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid