w3resource

C#: Count the number of duplicate characters in a string

C# Sharp String: Exercise-60 with Solution

Write a C# Sharp program to count the number of duplicate characters (case sensitive) including spaces in a given string. If there are no duplicates, return 0.

Sample Data:
("Red Green WHITE") -> 2
("ppqrrsttuuu") -> 4
("dow jones industrial average") -> 9

Sample Solution-1:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {

            string text = "Red Green WHITE";
            Console.WriteLine("Original String: " + text);
            Console.WriteLine("Number of duplicate characters in the said string: " + test(text));
            text = "ppqrrsttuuu";
            Console.WriteLine("Original String: " + text);
            Console.WriteLine("Number of duplicate characters in the said string: " + test(text));
            text = "dow jones industrial average";
            Console.WriteLine("Original String: " + text);
            Console.WriteLine("Number of duplicate characters in the said string: " + test(text));
        }

        public static int test(string text)
        {
            return text.GroupBy(n => n).Count(n => n.Count() >= 2);
        }
    }
}

Sample Output:

Original String: Red Green WHITE
Number of duplicate characters in the said string: 2
Original String: ppqrrsttuuu
Number of duplicate characters in the said string: 4
Original String: dow jones industrial average
Number of duplicate characters in the said string: 9

Flowchart :

Flowchart: C# Sharp Exercises - Count the number of duplicate characters in a string.

Sample Solution-2:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {

            string text = "Red Green WHITE";
            Console.WriteLine("Original String: " + text);
            Console.WriteLine("Number of duplicate characters in the said string: " + test(text));
            text = "ppqrrsttuuu";
            Console.WriteLine("Original String: " + text);
            Console.WriteLine("Number of duplicate characters in the said string: " + test(text));
            text = "dow jones industrial average";
            Console.WriteLine("Original String: " + text);
            Console.WriteLine("Number of duplicate characters in the said string: " + test(text));
        }

        public static int test(string text)
        {
            var t = from x in text
                      group x by x into gr
                      where gr.Count() > 1
                      select gr;

            return (t.Count());
        }
    }
}

Sample Output:

Original String: Red Green WHITE
Number of duplicate characters in the said string: 2
Original String: ppqrrsttuuu
Number of duplicate characters in the said string: 4
Original String: dow jones industrial average
Number of duplicate characters in the said string: 9

Flowchart :

Flowchart: C# Sharp Exercises - Count the number of duplicate characters in a string.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Reverse an integer and add with the original number.
Next C# Sharp Exercise: Count Occurrences of a substring in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook