Click here to Skip to main content
12,820,894 members (29,186 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 29 Dec 2013

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.


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#.


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


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


About the Author

Sk. Saiful Alam
Web Developer
Bangladesh Bangladesh
I am a web developer. I develop web application using c# and coldfusion. I am a tech blogger since 2007. I published more than 3000 c# 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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
Doug Domeny31-Dec-13 10:29
professionalDoug Domeny31-Dec-13 10:29 
QuestionSuggestion... Pin
Jason Vogel30-Dec-13 7:09
memberJason Vogel30-Dec-13 7:09 
AnswerRe: Suggestion... Pin
Sk. Saiful Alam30-Dec-13 7:28
memberSk. Saiful Alam30-Dec-13 7:28 
AnswerRe: Suggestion... Pin
johannesnestler9-Jan-14 4:56
memberjohannesnestler9-Jan-14 4:56 
GeneralMy vote of 2 Pin
leiyangge29-Dec-13 15:22
memberleiyangge29-Dec-13 15:22 
GeneralRe: My vote of 2 Pin
Klaus Luedenscheidt29-Dec-13 19:20
memberKlaus Luedenscheidt29-Dec-13 19: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.

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