How to get distinct characters?

前端 未结 9 1933
南旧
南旧 2020-12-10 10:21

I have a code like

string code = \"AABBDDCCRRFF\";

In this code, I want to retrieve only distinct characters

The Output should be l

相关标签:
9条回答
  • 2020-12-10 11:15
    string code = "AABBDDCCRRFF";
    string answer = new String(code.Distinct().ToArray());
    
    0 讨论(0)
  • 2020-12-10 11:19

    How about in linq:

    var str = "AABBCCDDDDDDEEEEEFFF";
    var unique = str.ToCharArray().Distinct();
    Console.WriteLine("Answer: {0}.", string.Join(string.Empty, unique));
    
    0 讨论(0)
  • 2020-12-10 11:21

    You can do this as follows

    var s = "AABBDDCCRRFF";
    var result = s.Distinct();
    var distinct = result.Aggregate(string.Empty, (current, c) => current + c.ToString());
    Console.WriteLine(distinct);
    
    0 讨论(0)
提交回复
热议问题