Question 1) Write function in c# to remove duplicates characters from string ?like:-a) “morningstar”—-> “mornigsta”b) “google”——> “gole”c) “Google”——> “Gogle”
Answer:-
class Program{ static void Main(string[] args) { string myStr = "morningstar"; Console.WriteLine("Initial String: " + myStr); var unique = new HashSet<char>(myStr); var Myfinalstring = ""; foreach (char c in unique) { Myfinalstring = Myfinalstring + c; } Console.WriteLine("New String after removing duplicates: " + Myfinalstring); Console.ReadKey(); }}
class Program
{
static void Main(string[] args)
string myStr = "morningstar";
Console.WriteLine("Initial String: " + myStr);
var unique = new HashSet<char>(myStr);
var Myfinalstring = "";
foreach (char c in unique)
Myfinalstring = Myfinalstring + c;
}
Console.WriteLine("New String after removing duplicates: " + Myfinalstring);
Console.ReadKey();
public static void RemoveDuplicate(string str) { string final = string.Empty;
for (int i = 0; i < str.Length; i++) { if(!final.Contains(str[i])) { final += str[i]; } } Console.WriteLine("The value after remove duplicate value is : " + final); }
for (int i = 0; i < str.Length; i++)
if(!final.Contains(str[i]))
final += str[i];
Console.WriteLine("The value after remove duplicate value is : " + final);
public static string RemoveDuplicatedCharacters(string s) { char[] chars = s.ToArray();
var dist = chars.Distinct().ToArray(); return new string(dist); }
var dist = chars.Distinct().ToArray();
return new string(dist);