Dictionary find duplicate values
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>
- How to use KeyValuePair in a Dictionary
- Best way to iterate over a Dictionary
- How to sort a Dictionary in ascending and descending order
- How to sort a Dictionary by DateTime value
- How to get key by index from a Dictionary
- How to perform foreach loop through Dictionary keys
- How to iterate through a Dictionary using for loop
- How to create a Dictionary from a list
- How to reverse a Dictionary items
- How to convert a Dictionary to a string