How to get distinct characters?

前端 未结 9 1931
南旧
南旧 2020-12-10 10:21

I have a code like

string code = \"AABBDDCCRRFF\";

In this code, I want to retrieve only distinct characters

The Output should be l

9条回答
  •  醉梦人生
    2020-12-10 11:07

    To get all distinct characters I have used ascii array. steps 1. Added ascii array of size 256, becuase max char ascii value is 255 step 2. increment each ascii to the location of particular alphabet's ascii value

    please refer below code to understand.

    public static class StringManipulation
    {
      public static string UniqueChars(this string astrInputString)
      {
           astrInputString = astrInputString.ToLower();
        int[] arrAscii = new int[256];
        string lstrFinalOutput = string.Empty;
        for (int i = 0; i < astrInputString.Length; i++)
        {
            arrAscii[Convert.ToInt32(astrInputString[i])]++;
        }
    
        for (int i = 0; i < astrInputString.Length; i++)
        {
            if (arrAscii[Convert.ToInt32(astrInputString[i])] == 1)
                lstrFinalOutput = lstrFinalOutput + astrInputString[i];
        }
        return lstrFinalOutput;
      }
    }
    class Program
    {
      static void Main(string[] args)
      {
        string input = "AAssjjkkLLlx";
        Console.WriteLine("Unique Charcters in string " + input.UniqueChars());
    
      }
    }
    

    Click to Download this Code https://github.com/SiddharthBhamare/StringManipulation/blob/master/StringManipulation/Program.cs

提交回复
热议问题