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!

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Adam Weitzman
  • 1,552
  • 6
  • 23
  • 45
  • http://stackoverflow.com/questions/245369/how-do-i-get-an-array-of-repeated-characters-from-a-string-using-linq – Nick G Oct 31 '16 at 15:39
  • remove string distinct from the input string (turtle) and get the delete characters. see this - http://stackoverflow.com/questions/2201595/c-sharp-simplest-way-to-remove-first-occurance-of-a-substring-from-another-str – Wasi Ahmad Oct 31 '16 at 15:39

2 Answers2

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();
Pankaj
  • 9,749
  • 32
  • 139
  • 283
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
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.

UtopiaLtd
  • 2,520
  • 1
  • 30
  • 46