Ajit More
Write function in c# to remove duplicates characters from string ?

Question 1) Write function in c# to remove duplicates characters from string ?
like:-
a) “morningstar”—-> “mornigsta”
b) “google”——> “gole”
c) “Google”——> “Gogle”

Answer:-

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string myStr = "morningstar";
  6. Console.WriteLine("Initial String: " + myStr);
  7. var unique = new HashSet<char>(myStr);
  8. var Myfinalstring = "";
  9. foreach (char c in unique)
  10. {
  11. Myfinalstring = Myfinalstring + c;
  12. }
  13. Console.WriteLine("New String after removing duplicates: " + Myfinalstring);
  14. Console.ReadKey();
  15. }
  16. }
By Ajit More in .NET on Jan 31 2020
  • Sahariar Hussain
    Sep, 2020 29

    public static void RemoveDuplicate(string str)
    {
    string final = string.Empty;

    1. for (int i = 0; i < str.Length; i++)
    2. {
    3. if(!final.Contains(str[i]))
    4. {
    5. final += str[i];
    6. }
    7. }
    8. Console.WriteLine("The value after remove duplicate value is : " + final);
    9. }

    • 2
  • Imogen Harden
    Jul, 2020 21

    public static string RemoveDuplicatedCharacters(string s)
    {
    char[] chars = s.ToArray();

    1. var dist = chars.Distinct().ToArray();
    2. return new string(dist);
    3. }

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS