How can you remove duplicate characters in a string?

前端 未结 20 2296
离开以前
离开以前 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:10

    Console.WriteLine("Enter String");
    
    string str = Console.ReadLine();
    
    string result = "";
    result += str[0]; // first character of string
    
    for (int i = 1; i < str.Length; i++)
    {
        if (str[i - 1] != str[i])
            result += str[i];
    }
    
    Console.WriteLine(result);
    

提交回复
热议问题