Algorithm to remove duplicate characters from a string: C#
Algorithm to remove duplicate characters from a string: C#
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the String");
string inp = Console.ReadLine();
string du = RemoveDuplicates(inp);
Console.WriteLine(du);
Console.ReadKey();
}
static string RemoveDuplicates(string input)
{
StringBuilder result=new StringBuilder();
char[] chars = input.ToCharArray();
for(int i=0;i<chars.Length;i++)
{
int counter = 0;
for (int j=i+1; j<chars.Length;j++)
{
if (chars[i] == chars[j])
counter++;
}
if(counter<1)
result.Append(chars[i]);
}
return result.ToString();
}
}
Please add your comments if it helps....
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the String");
string inp = Console.ReadLine();
string du = RemoveDuplicates(inp);
Console.WriteLine(du);
Console.ReadKey();
}
static string RemoveDuplicates(string input)
{
StringBuilder result=new StringBuilder();
char[] chars = input.ToCharArray();
for(int i=0;i<chars.Length;i++)
{
int counter = 0;
for (int j=i+1; j<chars.Length;j++)
{
if (chars[i] == chars[j])
counter++;
}
if(counter<1)
result.Append(chars[i]);
}
return result.ToString();
}
}
Please add your comments if it helps....
Comments
Post a Comment