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!
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