HOW TO DELETE DUPLICATE ITEMS IN THE LIST IN C#.

225 views
Skip to first unread message

TARAK

unread,
Nov 25, 2008, 5:27:37 AM11/25/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
HOW TO DELETE DUPLICATE ITEMS IN THE LIST IN C#.

the name of the list is "DBInstanceSpecificAssetAccessList" having 10
items.

But it has same records in several rows.

for example : DBInstanceSpecificAssetAccessList[0]----->"TARAK"
DBInstanceSpecificAssetAccessList[3]----->"TARAK"
DBInstanceSpecificAssetAccessList[4]----->"TARAK"
DBInstanceSpecificAssetAccessList[5]----->"AMIT"
DBInstanceSpecificAssetAccessList[6]----->"AMIT"

HOW CAN I DELETE THE DUPLICATE ITEMS ....

CAN ANY ONE PLEASE SUGGEST ME WHAT I NEED TO DO...


WITH REGARDS

Cerebrus

unread,
Nov 25, 2008, 12:58:08 PM11/25/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Not until you learn to type in proper case.

rhaazy

unread,
Nov 25, 2008, 1:08:08 PM11/25/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
static List<string> removeDuplicates(List<string> inputList)
{
Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
List<string> finalList = new List<string>();

foreach (string currValue in inputList)
{
if (!uniqueStore.ContainsKey(currValue))
{
uniqueStore.Add(currValue, 0);
finalList.Add(currValue);
}
}
return finalList;
}

You may have to adjust this to meet your specific needs, but the
concept is simple enough to demonstrate.
You need to create a new list of the same type and asign it the value
of what is returned from the removeDuplicates function.
> > WITH REGARDS- Hide quoted text -
>
> - Show quoted text -

AstroDrabb

unread,
Nov 25, 2008, 10:23:33 PM11/25/08
to DotNetDe...@googlegroups.com
On Tue, Nov 25, 2008 at 5:27 AM, TARAK <tara...@gmail.com> wrote:

HOW TO DELETE DUPLICATE ITEMS IN THE LIST IN C#.

 the name of the list is "DBInstanceSpecificAssetAccessList" having 10
items.

Did you really, really name a variable "DBInstanceSpecificAssetAccessList"?

Muhammad Arif

unread,
Nov 26, 2008, 1:00:51 AM11/26/08
to DotNetDe...@googlegroups.com

Little Changes in the rhaazy Function 

List<string> removeDuplicates(List<string> inputList)
replace the parameter List<string> inputlist to IList<string> inputList

List<string> removeDuplicates(IList<string> inputList)

the rest works fine.

Regards
--
Muhammad Arif
Software Engineer
+923009347315

shini gupta

unread,
Nov 26, 2008, 2:43:13 AM11/26/08
to DotNetDe...@googlegroups.com
U can do it by storeProc simply.
Select Distinct values only whn bind the list.

CK

unread,
Nov 26, 2008, 4:08:22 AM11/26/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Slightly better version (untested):

static List<T> removeDuplicates(List<T> inputList)
{
Dictionary<T, int> uniqueStore = new Dictionary<T, int>();
List<T> finalList = new List<T>();

foreach (T currValue in inputList)
> > - Show quoted text -- Hide quoted text -

Cerebrus

unread,
Nov 26, 2008, 1:47:27 PM11/26/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I bet it's one of the shorter ones ! :P

On Nov 26, 8:23 am, AstroDrabb <astrodr...@gmail.com> wrote:

asit

unread,
Nov 26, 2008, 2:55:33 PM11/26/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
static List<T> removeDuplicates(List<T> inputList)
{
Dictionary<T, int> uniqueStore = new Dictionary<T, int>();
List<T> finalList = new List<T>();

foreach (T currValue in inputList)
{
if (!uniqueStore.ContainsKey(currValue))
{
uniqueStore.Add(currValue, 0);
finalList.Add(currValue);
}
}
return finalList;

}

before calling the above function, sort the list. It will be faster.

CK

unread,
Nov 27, 2008, 4:38:19 AM11/27/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
sorry, I win :)
Reply all
Reply to author
Forward
0 new messages