Duplicate words in a string

Write a program to determine the duplicate words in a string

Solution:

using System;

namespace DuplicateWord
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter a string:");
            var str = Console.ReadLine();

            var wordArray = str.Split(' ');

            Console.WriteLine("Duplicate words:");
            for (var i = 0; i < wordArray.Length; i++)
            {
                var count = 1;
                for (var j = i + 1; j < wordArray.Length; j++)
                {
                    if (string.Compare(wordArray[i], wordArray[j], StringComparison.CurrentCultureIgnoreCase) != 0) continue;
                    count++;
                    wordArray[j] = "0";
                }

                if (count > 1 && wordArray[i] != "0")
                    Console.WriteLine(wordArray[i]);
            }
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *