Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Removing Duplicates in the Collection

I had to remove the duplicates from the collection so I wrote this piece of code which creates a sublist and returns it to the caller.

public List<T> RemoveDuplicates()

{

List<T> subList = new List<T>();

foreach (T item in this)

{

if (!subList.Contains(item))

{

subList.Add(item);

}

}

return subList;

}

I was wondering if there are better ways to remove the duplicates. I am looking forward to C# 3.0 where you can remove the duplicates with the following code:

string[] names = {"AzamSharp","Alex","Scott","Mary","AzamSharp","Scott","John","Doe","Scott" };

List<IEnumerable> distinctNames = names.Distinct();

Nice!

Sponsor

Published Sunday, November 05, 2006 10:09 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Web Links 11.13.2006 &laquo; Rhonda Tipton&#8217;s WebLog @ Monday, November 13, 2006 9:03 AM

PingBack from http://rtipton.wordpress.com/2006/11/13/web-links-11132006/

Web Links 11.13.2006 « Rhonda Tipton’s WebLog

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below