How to get distinct characters?

前端 未结 9 1932
南旧
南旧 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:02

    Linq's Distinct returns distinct elements from a sequence. As the String class implements IEnumerable<char>, Distinct in this context returns an IEnumerable<char> containing all of the unique characters in the string.

    code.Distinct();
    
    0 讨论(0)
  • 2020-12-10 11:05

    Here's an approach that provides the string and a count of the distinct characters...

    using System;
    using System.Collections.Generic;
    
    namespace StringDict
    {
        class Program
        {
            static IDictionary<char, int> charDict =new Dictionary<char, int>();
            static private string _charStr = "s;ldfjgsl;dkkjfg;lsdkfjg;lsdkfjg;lsdkfjg;lsdkfj";
            private static int _outInt=0;
            static void Main(string[] args)
            {
                foreach (var ch in _charStr)
                {
                    if (!charDict.TryGetValue(ch,out _outInt))
                    {
                        charDict.Add(new KeyValuePair<char, int>(ch,1));
                    }
                    else
                    {
                        charDict[ch]++;
                    }
                }
                Console.Write("Unique Characters: ");
                Console.WriteLine('\n');
                foreach (var kvp in charDict)
                {
                    Console.Write(kvp.Key);
                }
                Console.WriteLine('\n');
                foreach (var kvp in charDict)
                {
                    Console.WriteLine("Char: "+kvp.Key+" Count: "+kvp.Value);
                }
                Console.ReadLine();
            }
        }
    }
    

    Output...

    Unique Characters:
    
    s;ldfjgk
    
    Char: s Count: 6
    Char: ; Count: 6
    Char: l Count: 6
    Char: d Count: 6
    Char: f Count: 6
    Char: j Count: 6
    Char: g Count: 5
    Char: k Count: 6
    
    0 讨论(0)
  • 2020-12-10 11:07

    To get all distinct characters I have used ascii array. steps 1. Added ascii array of size 256, becuase max char ascii value is 255 step 2. increment each ascii to the location of particular alphabet's ascii value

    please refer below code to understand.

    public static class StringManipulation
    {
      public static string UniqueChars(this string astrInputString)
      {
           astrInputString = astrInputString.ToLower();
        int[] arrAscii = new int[256];
        string lstrFinalOutput = string.Empty;
        for (int i = 0; i < astrInputString.Length; i++)
        {
            arrAscii[Convert.ToInt32(astrInputString[i])]++;
        }
    
        for (int i = 0; i < astrInputString.Length; i++)
        {
            if (arrAscii[Convert.ToInt32(astrInputString[i])] == 1)
                lstrFinalOutput = lstrFinalOutput + astrInputString[i];
        }
        return lstrFinalOutput;
      }
    }
    class Program
    {
      static void Main(string[] args)
      {
        string input = "AAssjjkkLLlx";
        Console.WriteLine("Unique Charcters in string " + input.UniqueChars());
    
      }
    }
    

    Click to Download this Code https://github.com/SiddharthBhamare/StringManipulation/blob/master/StringManipulation/Program.cs

    0 讨论(0)
  • 2020-12-10 11:09

    Just for the hell of it - using a StringBuilder

     string code = "AABBDDCCRRFF";
     StringBuilder sb = new StringBuilder();
     code.Distinct().ToList().ForEach(c => sb.Append(c));
     MessageBox.Show(sb.ToString());
    
    0 讨论(0)
  • 2020-12-10 11:10

    Here's an approach with grouping by characters...

    var code="AABBDDCCRRFF";
    var unique = string.Concat(text.GroupBy(a => a).Select(a => a.Key));
    
    0 讨论(0)
  • 2020-12-10 11:13

    Alternative option using dotNet 2.0 compatible code:

    public string RemoveDuplicateChars(string input)
    {
        var stringBuilder = new StringBuilder(input);
    
        foreach (char c in input)
        {
            stringBuilder.Replace(c.ToString(), string.Empty)
                         .Append(c.ToString());
        }
    
        return stringBuilder.ToString();
    }
    
    0 讨论(0)
提交回复
热议问题