1

I have:

string turtle = "turtle"
var charArray = turtle.ToCharArray()

When I do:

var distinct = charArray.Distinct().ToArray()
// distinct = ["t","u","r","l","e"]

My question is:

How do I save the characters that got deleted from charArray when I called Distinct on it? How do I get the variable distinct to equal "t" (the character that was removed)

Thanks!

2

2 Answers 2

2

Use GroupBy to return only the letters having count > 1:

string turtle = "turtle";
var dups = (from l in turtle
            group 1 by l into g
            where g.Count() > 1
            select g.Key).ToList();
Sign up to request clarification or add additional context in comments.

Comments

1
var nonUniqueChars = charArray.GroupBy(x => x)
.Where(x => x.Count() > 1)
.Select(x => x.First())

This will organize group all the characters that appear, find the ones that appear more than once, and then pull a single instance of each such character.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.