How can you remove duplicate characters in a string?

前端 未结 20 2234
离开以前
离开以前 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:09
    String str="AABBCANCDE";  
    String newStr=""; 
    for( int i=0; i<str.length(); i++)
    {
     if(!newStr.contains(str.charAt(i)+""))
     newStr= newStr+str.charAt(i);
     }
     System.out.println(newStr);
    
    0 讨论(0)
  • 2020-12-05 17:10
    char* removeDups(const char* str)
    {
     char* new_str = (char*)malloc(256*sizeof(char));
     int i,j,current_pos = 0,len_of_new_str;
     new_str[0]='\0';
    
     for(i=0;i<strlen(str);i++)
    {
     len_of_new_str = strlen(new_str);
    for(j=0;j<len_of_new_str && new_str[j]!=str[i];j++)
       ;
      if(j==len_of_new_str)
       {
         new_str[len_of_new_str] = str[i];
         new_str[len_of_new_str+1] = '\0';
       }
    }
      return new_str;
    }
    

    Hope this helps

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-05 17:12

    //this is in C#, validation left out for brevity //primitive solution for removing duplicate chars from a given string

        public static char[] RemoveDup(string s)
        {
            char[] c = new char[s.Length];
            int unique = 0;
            c[unique] = s[0];  // Assume: First char is trivial
            for (int i = 1; i < s.Length; i++)
            {
                if (s[i-1] != s[i]
            c[++unique] = s[i];
            }
            return c;
        }
    
    0 讨论(0)
  • 2020-12-05 17:12

    I like Quintin Robinson answer, only there should be some improvements like removing List, because it is not necessarry in this case. Also, in my opinion Uppercase char ("K") and lowercase char ("k") is the same thing, so they should be counted as one.

    So here is how I would do it:

    private static string RemoveDuplicates(string textEntered)
        {
    
            string newString = string.Empty;
    
            foreach (var c in textEntered)
            {
                if (newString.Contains(char.ToLower(c)) || newString.Contains(char.ToUpper(c)))
                {
                    continue;
                }
                newString += c.ToString();
            }
            return newString;
        }
    
    0 讨论(0)
  • 2020-12-05 17:13

    you may use HashSet:

     static void Main()
        {
            string textWithDuplicates = "aaabbcccggg";
    
            Console.WriteLine(textWithDuplicates.Count());  
            var letters = new HashSet<char>(textWithDuplicates);
            Console.WriteLine(letters.Count());
    
            foreach (char c in letters) Console.Write(c);   
        }
    
    0 讨论(0)
提交回复
热议问题