How can you remove duplicate characters in a string?

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

    Revised version of the first answer i.e: You don't need ToCharArray() function for this to work.

    public static string RemoveDuplicates(string input)
    {
        return new string(input.Distinct().ToArray());
    }
    
    0 讨论(0)
  • 2020-12-05 17:03

    My answer in java language.
    Posting here so that you might get a idea even it is in Java language.Algorithm would remain same.

    public String removeDup(String s)
      {
        if(s==null) return null;
        int l = s.length();
        //if length is less than 2 return string
        if(l<2)return s;
        char arr[] = s.toCharArray();
    
        for(int i=0;i<l;i++)
        {
          int j =i+1; //index to check with ith index
          int t = i+1; //index of first repetative char.
    
          while(j<l)
          {
            if(arr[j]==arr[i])
            {
              j++;
    
            }
            else
            {
              arr[t]=arr[j];
              t++;
              j++;
            }
    
          }
          l=t;
        }
    
        return new String(arr,0,l);
      }
    
    0 讨论(0)
  • 2020-12-05 17:07

    A Linq approach:

    public static string RemoveDuplicates(string input)
    {
        return new string(input.ToCharArray().Distinct().ToArray());
    }
    
    0 讨论(0)
  • 2020-12-05 17:07

    Below is the code to remove duplicate chars from a string

            var input = "SaaSingeshe";
            var filteredString = new StringBuilder();
            foreach(char c in input)
            {
                if(filteredString.ToString().IndexOf(c)==-1)
                {
                    filteredString.Append(c);
                }
            }
            Console.WriteLine(filteredString);
            Console.ReadKey();
    
    0 讨论(0)
  • 2020-12-05 17:08

    For arbitrary length strings of byte-sized characters (not for wide characters or other encodings), I would use a lookup table, one bit per character (32 bytes for a 256-bit table). Loop through your string, only output characters that don't have their bits turned on, then turn the bit on for that character.

    string removedupes(string s)
    {
        string t;
        byte[] found = new byte[256];
        foreach(char c in s)
        {
            if(!found[c]) {
                t.Append(c);
                found[c]=1;
            }
        }
        return t;
    }
    

    I am not good with C#, so I don't know the right way to use a bitfield instead of a byte array.

    If you know that your strings are going to be very short, then other approaches would offer better memory usage and/or speed.

    0 讨论(0)
  • 2020-12-05 17:08

    It sounds like homework to me, so I'm just going to describe at a high level.

    • Loop over the string, examining each character
    • Check if you've seen the character before
      • if you have, remove it from the string
      • if you haven't, note that you've now seen that character
    0 讨论(0)
提交回复
热议问题