5

I have a List of custom types where I want to remove the duplicate and the original if a duplicate is found. Can only be one possible duplicate.

I can overide Equals and GetHashCode and then use Distinct but this only removes the duplicate. I need to remove both original and duplicate... Any ideas for something elegant so I don't have to use a hammer.

2
  • 1
    post the relevant code. Commented May 16, 2013 at 20:41
  • 1
    can you provide a coded example of what it is you are working with this will actually help others to gain an understanding of what you are trying to do Commented May 16, 2013 at 20:41

2 Answers 2

6

You can use GroupBy, followed by Where (g => g.Count() == 1) to filter out all records that have duplicates:

var res = orig.GroupBy(x => x).Where(g => g.Count() == 1).Select(g => g.Key);

In order for this to work, you still need to override GetHashCode and Equals.

Sign up to request clarification or add additional context in comments.

Comments

3
var itemsExistingExactlyOnce = list.GroupBy(x => x)
    .Where(group => group.Count() == 1)
    .Select(group => group.Key);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.