[ACCEPTED]-Find object data duplicates in List of objects-.net

Accepted answer
Score: 25

This gets you the duplicated SSN:

var duplicatedSSN =
    from p in persons
    group p by p.SSN into g
    where g.Count() > 1
    select g.Key;

The duplicated 2 list would be like:

var duplicated = persons.FindAll( p => duplicatedSSN.Contains(p.SSN) );

And then just iterate 1 over the duplicates and remove them.

duplicated.ForEach( dup => persons.Remove(dup) ); 
Score: 3

Thanks to gcores for getting me started 2 down a correct path. Here's what I ended 1 up doing:

var duplicatedSSN =
    from p in persons
    group p by p.SSN into g
    where g.Count() > 1
    select g.Key;

var duplicates = new List<Person>();

foreach (var dupeSSN in duplicatedSSN)
{
    foreach (var person in persons.FindAll(p => p.SSN == dupeSSN))
        duplicates.Add(person);
}

duplicates.ForEach(dup => persons.Remove(dup));
Score: 1
List<Person> actualPersons = persons.Distinct().ToList();
List<Person> duplicatePersons = persons.Except(actualPersons).ToList();

0

Score: 1

Based on the recommendation by @gcores above.

If 6 you want to add a single object of the duplicated 5 SSN back to the list of persons, then add 4 the following line:

IEnumerable<IGrouping<string, Person>> query = duplicated.GroupBy(d => d.SSN, d => d);

        foreach (IGrouping<string, Person> duplicateGroup in query)
        {
            persons.Add(duplicateGroup .First());
        }

My assumption here is 3 that you may only want to remove duplicate 2 values minus the original value that the 1 duplicates derived from.

Score: 0

well if you implement IComparable like so:

int IComparable<Person>.CompareTo(Person person)
{
    return this.SSN.CompareTo(person.SSN);
}

then 1 a comparison like the following will work:

for (Int32 i = 0; i < people.Count; i++)
{
    for (Int32 j = 1; j < items.Count; j++)
    {
        if (i != j && items[i] == items[j])
        {
            // duplicate
        }
    }
}
Score: 0

Traverse the list and keep a Hashtable of 3 SSN/count pairs. Then enumerate your table 2 and remove the items that match SSNs where 1 SSN count > 0.

Dictionary<string, int> ssnTable = new Dictionary<string, int>();

foreach (Person person in persons)
{
   try
   {
      int count = ssnTable[person.SSN];
      count++;
      ssnTable[person.SSN] = count;
   }
   catch(Exception ex)
   {
       ssnTable.Add(person.SSN, 1);
   }
}

// traverse ssnTable here and remove items where value of entry (item count) > 1
Score: 0

Does persons have to be a List<Person>? What if it were a 4 Dictionary<int, Person>?

var persons = new Dictionary<int, Person>();

...

// For each person you want to add to the list:
var person = new Person
{
  ...
};

if (!persons.ContainsKey(person.SSN))
{
  persons.Add(person.SSN, person);
}

// If you absolutely, positively got to have a List:
using System.Linq;
List<Person> personsList = persons.Values.ToList();

If you are working with unique instances 3 of Person (as opposed to different instances that 2 might happen to have the same properties), you 1 might get better performance with a HashSet.

More Related questions