c# - How to find duplicate values from a Dictionary

Find duplicate values from a dictionary
The Dictionary class represents a collection of keys and values. The .net framework’s Dictionary is located under the System.Collections.Generic namespace. The Dictionary object constructor is Dictionary<TKey,TValue>. The TKey is the data type of the keys in the Dictionary and the TValue is the data type of the values in the Dictionary. We can initialize an empty Dictionary instance and add elements to it using its Add() method. We also can add some items to the Dictionary at the initializing time.

The following .net c# tutorial code demonstrates how we can find duplicate values from a Dictionary. Dictionary keys are unique, we can’t put multiple elements in a Dictionary with the same key. But Dictionary values can be duplicated. The .net developers can set the same value to multiple elements. So, sometimes the .net developers need to find the duplicate values from a Dictionary items collection.

To find the duplicate values from a Dictionary object, we have to group Dictionary items whose value exists more than one time. The Enumerable GroupBy() method groups the elements of a sequence.

In this expression, we applied a condition while grouping Dictionary items. The condition is value exists more than one time. Finally, we get the Dictionary duplicate values using GroupBy() method and we can loop through the filtered items collection to display it on the user interface.
dictionary-find-duplicate-values.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
    
<!DOCTYPE html>      
<script runat="server">    
    protected void Button1_Click(object sender, System.EventArgs e)  
    {  
        //initialize a dictionary with keys and values.  
        Dictionary<int, string> birds = new Dictionary<int, string>() {  
            {1,"Golden Pheasant"},  
            {2,"Southern Screamer"},  
            {3,"Golden Pheasant"},  
            {4,"Swan Goose"},  
            {5,"Swan Goose"},  
            {6,"Golden Pheasant"},  
            {7,"Greylag Goose"}  
        };

        Label1.Text = "dictionary elements..........";
        foreach (KeyValuePair<int, string> pair in birds)
        {
            Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
        }

        //get dictionary duplicate values.
        var duplicatesValue = birds.GroupBy(x => x.Value).Where(x => x.Count() > 1);

        Label1.Text += "<br /><br />dictionary duplicate values..........<br />";  
        foreach(var item in duplicatesValue)
        {
            Label1.Text += item.Key + "<br />";
        }
    }      
</script>      
      
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>c# example - dictionary find duplicate values</title>      
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            c# example - dictionary find duplicate values
        </h2>      
        <hr width="550" align="left" color="Gainsboro" />      
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Font-Size="Large"    
            >      
        </asp:Label>      
        <br /><br />    
        <asp:Button       
            ID="Button1"       
            runat="server"       
            Text="dictionary find duplicate values"      
            OnClick="Button1_Click"    
            Height="40"      
            Font-Bold="true"      
            />      
    </div>      
    </form>      
</body>      
</html>
More c# examples