How to remove duplicate letters from string in C#

3 Answers

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main() {
        string str = "csharp java c c++ php";

        var hashset = new HashSet<char>(str);

        str = string.Join("", hashset);
        
        Console.WriteLine(str);
    }
}



/*
run:

csharp jv+

*/

 



answered Jul 18, 2022 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        string str = "csharp java c c++ php";

        string tmp = string.Empty;
        
        for (int i = 0; i < str.Length; i++) {
            if (!tmp.Contains(str[i])) {
                tmp += str[i];
            }
        }
        
        str = tmp;
        
        Console.WriteLine(str);
    }
}



/*
run:

csharp jv+

*/

 



answered Jul 18, 2022 by avibootz
0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        string str = "csharp java c c++ php";

        var uniqueCharArray = str.ToCharArray().Distinct().ToArray();
        
        str = new string(uniqueCharArray);

        Console.WriteLine(str);
    }
}



/*
run:

csharp jv+

*/

 



answered Jul 18, 2022 by avibootz

Related questions

...