Remove duplicate characters from
String?
A string Can Contained two
or more character in it, but we want to have only one
For Example
Required input and output
Input: Csharpstar
Output: Csharpt
Output: Csharpt
Input: Ganga
Output: Gang
Output: Gang
Input: Yahoo
Output: Yaho
Output: Yaho
Input: ganga
Output: gan
Output: gan
Solution:
You can achieve this by two ways
1.
Using index of method IndexOf(ch)
Step 1:
public string RemoveDuplicateChar(String Str)
{
string result = "";
Str = Str.ToUpper();
foreach (char value in Str)
{
// See if character is in the table.
if (result.IndexOf(value) == -1)
{
result += value;
}
}
return result;
}
static void Main(string[] args)
{
String Name = "Ganga";
Program obj = new Program();
//Console.WriteLine("Total
Duplicate value"+ obj.DuplicateChar(Name));
Console.WriteLine("New String = " + obj.RemoveDuplicateChar(Name));
Console.ReadLine();
}
No comments:
Post a Comment