In this example, you will see that how to find out duplicate strings using linq in asp.net. In order to find duplicate string we will use GroupBy method and if any group has a count greater then 1, it’s is a duplicate.
Here’s an example.
First, I have created Find-duplicate.aspx page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Find duplicate string using Linq in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Find-Duplicate.aspx.cs code behind script.
C#
protected void Page_Load(object sender, EventArgs e)
{
String[] List = { "Aamir Hasan", "Google", "Yahoo", "Hotmail", "Google", "Yahoo" };
var Duplicates = List.GroupBy(val => val)
.Where(g => g.Count() > 1)
.Select(g => g.Key);
foreach (var d in Duplicates)
Label1.Text += d + "<br>";
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim List As [String]() = {"Aamir Hasan", "Google", "Yahoo", "Hotmail", "Google", "Yahoo"}
Dim Duplicates = List.GroupBy(Function(val) val)
.Where(Function(g) g.Count() > 1)
.[Select](Function(g) g.Key)
For Each d As String In Duplicates
Label1.Text += d + "<br>"
Next
End Sub
Output

Download
Find-Duplicate.rar (844.00 bytes)
See Live demo