Click here to Skip to main content
16,006,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I am Nagarajan a software solution developer. I have one doubt.
I am using a data grid control, and retrieved email textbox item, and stored it into the array list.
How can I delete duplicate elements in the array list?
I'm using ASP.net and C#.
Kindly look at this and reply.

Thank you!

Regards,

Nagarajan P.
Posted
Updated 23-Dec-10 4:48am
v4
Comments
Manfred Rudolf Bihy 23-Dec-10 10:47am    
Edited for spelling and grammar.

Create an extension method called AddIfUnique. This extension method would do the uniqueness check for you, and all you have to do is pass in the data you want to add. I do this kind of thing all the time.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 23-Dec-10 11:43am    
Dang! I should have noticed that! He didn't mention the .NET version though he's using. Still... +5
Hi Nagarajan,

when you are adding the elements to the ArrayList call the method Contains to check if that element is already in the list. Add the element to said ArrayList only if it is not contained in the list. See my example code below:

C#
pulic bool function AddIfNotContained(Object element, ArrayList list)
{
    bool wasAdded = false;
    if(!list.Contains(element))
    {
        list.Add(element);
        wasAdded = true;
    }
    return wasAdded;
}


That should solve your problem.

Regards,

Manfred
 
Share this answer
 
v2
Why not stop the problem at the source by not allowing duplicates in the Array in the first list?

Otherwise, lots of different solutions here

http://stackoverflow.com/questions/9673/remove-duplicates-from-array[^]

The HashSet example is nice, depends if you're on >= .Net 3.5
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900