How can you remove duplicate characters in a string?

前端 未结 20 2236
离开以前
离开以前 2020-12-05 16:39

I have to implements a function that takes a string as an input and finds the non-duplicate character from this string.

So an an example is if I pass string str = \"

相关标签:
20条回答
  • 2020-12-05 17:13
     class Program
        {
            static void Main(string[] args)
            {
                bool[] doesExists = new bool[256];
                String st = Console.ReadLine();
                StringBuilder sb = new StringBuilder();
                foreach (char ch in st)
                {
                    if (!doesExists[ch])
                    {
                        sb.Append(ch);
                        doesExists[ch] = true;
                    }
                }
                Console.WriteLine(sb.ToString());
            }
        }
    
    0 讨论(0)
  • 2020-12-05 17:15

    namespace Demo { class Program {

      static void Main(string[] args) {
         string myStr = "kkllmmnnouo";
         Console.WriteLine("Initial String: "+myStr);
        // var unique = new HashSet<char>(myStr);
         HashSet<char> unique = new HashSet<char>(myStr);
         Console.Write("New String after removing duplicates: ");
    
         foreach (char c in unique) 
            Console.Write(c);   
      }    } }
    
    0 讨论(0)
提交回复
热议问题