I have a code like
string code = \"AABBDDCCRRFF\";
In this code, I want to retrieve only distinct characters
The Output should be l
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