-3

I am trying to get a user's input so that I can return how many duplicate characters they have.

This is how I got the input

  Console.WriteLine("Input a word to reveal duplicate letters");
        string input = Console.ReadLine();

For example, the code should return something like:

List of duplicate characters in String 'Programming'

g : 2

r : 2

m : 2

How do I find duplicate letters and count them in a string?

  • 2
    You need to try something! Its not that much complicated, defines step and go thorw it – Prashant Pimpale Oct 14 '19 at 12:35
  • I'm going to tell you two things, and you should be able to work out what to do: (1) `string` implements `IEnumerable`. (2) You can split an `IEnumerable` into groups of the same char using [`Enumerable.GroupBy()`](https://www.dotnetperls.com/groupby) – Matthew Watson Oct 14 '19 at 12:38
  • kindly check this [https://stackoverflow.com/questions/18547354/c-sharp-linq-find-duplicates-in-list] – Gnyasha Oct 14 '19 at 12:40
  • _exactly_ 2 as count? Or just an ordinary histogram? – Fildor Oct 14 '19 at 12:47

2 Answers2

1

Yes you can obtain this by using System.Linq GroupBy(), you going to group your string by character value and after filter the generated groups that have more than 1 values like so :

var word = "Hello World!";
var multipleChars = word.GroupBy(c => c).Where(group => group.Count() > 1);
foreach (var charGroup in multipleChars)
{
    Console.WriteLine(charGroup .Key + " : " + charGroup .Count());
}
Tiago Silva
  • 2,299
  • 14
  • 18
0

this will include case sensitivity as well as excluding non alphanumeric char

var sample = Console.ReadLine();
var letterCounter = sample.Where(char.IsLetterOrDigit)
                    .GroupBy(char.ToLower)
                    .Select(counter => new { Letter = counter.Key, Counter = counter.Count() })
                    .Where(c=>c.Counter>1);

foreach (var counter in letterCounter){
    Console.WriteLine(String.Format("{0} = {1}", counter.Letter,counter.Counter));
}