In this blog, we are going to learn how to get a duplicate word in a given string. Today(4/11/2017) a person posted a query to find the duplicate word from a textbox and wanted to display it on another textbox. For this reason, I am posting this blog for all the users who needs to apply the same logic in the future. It will be helpful to others.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace CountRepeatedWordCount
- {
- class Program
- {
- static void Main(string[] args)
- {
- string Word;
- Console.WriteLine("Enter the word!..");
- Word = Console.ReadLine();
- var Value = Word.Split(' ');
- Dictionary<string, int> RepeatedWordCount = new Dictionary<string, int>();
- for (int i = 0; i < Value.Length; i++)
- {
- if (RepeatedWordCount.ContainsKey(Value[i]))
- {
- int value = RepeatedWordCount[Value[i]];
- RepeatedWordCount[Value[i]] = value + 1;
- }
- else
- {
- RepeatedWordCount.Add(Value[i], 1);
- }
- }
- Console.WriteLine();
- Console.WriteLine("------------------------------------");
- Console.WriteLine("Repeated words and counts");
- foreach (KeyValuePair<string, int> kvp in RepeatedWordCount)
- {
- Console.WriteLine(kvp.Key + " Counts are " + kvp.Value);
- }
- Console.ReadKey();
- }
- }
- }
Step 1
Copy the code and paste it on C# console Application.
Note
Create the Application with same name CountRepeatedWordCount or alter the copied code, as per your Application name.
Step 2
Save and Run the Application. Now, you can enter the string and see the output.
Feel free to add your comments. If you have any query regarding this, feel free to post.