Peter
Peter

Reputation: 5332

How can I remove duplicates from one List<T> that are found in another?

This is for .NET 2.0, so I cannot use LINQ.

I have a bit of an interesting problem.

I am merging two lists of custom type "Article".

The below code does the job nicely:

List<Article> merge = new List<Article>(GetFeatureArticles());
merge.AddRange(result);

return merge;

GetFeatureArticle has only 2 items that are the first two elements in the merged list. "result" is large and its elements trail "GetFeatureArticle"'s elements.

The problem is that I need to compare the list returned from "GetFeatureArticles()" to the list in "result" and, if there is a match, remove the matched item in result, not in "GetFeatureArticles". Both lists are of type List<Article>.

I am limited by C# 2.0 unfortunately.

Thank you.

EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT

This is the implementation I ultimately went with as GetFeaturedArticles() will always be two items:

List<Article> k = new List<Article>(GetFeatureArticles());

            foreach (Article j in k)
            {
                for( int i = 0; i < tiles.Count; i++ )
                {
                    if (j.ID == tiles[i].ID)
                        tiles.Remove(tiles[i]);
                }
            }

            k.AddRange(tiles);

            return k;

Upvotes: 1

Views: 1939

Answers (5)

Vernard Sloggett
Vernard Sloggett

Reputation: 334

returnList = returnList.Union(ListToAdd).ToList();

or if combinging multiple lists of items...

foreach (List<ListItem> l in ListsToAdd)
{
    returnList = returnList.Union(l).ToList();
}

Upvotes: 1

Ian Nelson
Ian Nelson

Reputation: 58743

This works against .NET Framework 2.0:

var featured = new List<Article>(GetFeaturedArticles());

result.RemoveAll(featured.Contains);

result.InsertRange(0, featured);

return result;

Upvotes: 0

Ritch Melton
Ritch Melton

Reputation: 11598

Assuming you have some sort of object equality implemented.

var listA = new List<Article> {a, b, c, d};
var listB = new List<Article> {e, f}; //Where e, f are equal to b, c in a.
listA.RemoveAll(listB.Contains);

Upvotes: 4

Yahia
Yahia

Reputation: 70369

use

List<Article> g = new List<Article>(GetFeatureArticles());

foreach (Article a in g)
{
if (result.Contains (a))
    result.Remove (a);
}

g.AddRange (result);

return g;

EDIT - as per comments:

I assume that Article is a reference type and is implemented with suitable Equals and GetHashCode methods respectively - otherwise the above would only work for equal references (= "same object in both Lists")...

Upvotes: 1

Jim Mischel
Jim Mischel

Reputation: 133995

Build a Dictionary from the result set. Then:

foreach (Article a featuredArticles)
{
    if (resultsDictionary.ContainsKey(a.Key))
    {
        resultsDictionary.Remove(a.Key);
    }
}

Then convert the dictionary back to a list.

Upvotes: 0

Related Questions