Remove Duplicate characters from String in C#


 

Remove Duplicate characters from String in C#

Code

   public static void removeduplicateChar()

        {

            string s = "theDOTNetofFice";

            s = s.ToUpper();

            string newString = string.Empty;

            List<char> found = new List<char>();

            foreach (char c in s)

            {

                if (found.Contains(c))

                    continue;

 

                newString += c.ToString();

                found.Add(c);

            }

              Console.WriteLine("output is -" + newString);

            Console.ReadLine();

        }


Output




using LINQ and string constructor:


static string RemoveDuplicateChars(string key)
        {
            string result = new string(key.Distinct().ToArray());
            return result;
        }

Share this

Related Posts

Previous
Next Post »