简体   繁体   中英

How to list characters of a particular frequency in a string

例如,该场景是一个字符串“ THIS IS A TEST”,例如,将路径长度设置为2(取决于我们),结果应该为“ I”,因为这是唯一重复两次的字符串。长度设置为3,所以结果应该是(T,S,SPACES)。

string item = "THIS IS A TEST ";
List<char> result = item.GroupBy(x => x).Where(y => y.Count() == 2).Select(y => y.Key).ToList();

this returns a List of characters, appearing two times ( I )

I group all characters in the given string and compare the count of their appearence with 2

string content = "THIS IS A TEST ";

content.GroupBy(c => c).Where(c => c.Count() == 3).Select(c => c.Key);

An efficient way to do this is to create a lookup from number of repetitions to the list of characters repeating that many times:

var content = "this is a test";
var lookup = content
    .GroupBy(ch => ch)
    .ToLookup(grp => grp.Count, grp => grp.Key);

var twiceRepeatingChars = lookup[2];
var thriceRepeatingChars = lookup[3];
// and so on

This is a lengthy solution i have coded :) without group by.

public static void GetNumberOfTimesACharacterOccuredInAString(string text, int number)
{
        Dictionary<char, int> CharactorNumberOfOccurence = new Dictionary<char, int>();
        int count = 1;

        foreach (char c in text.ToLower())
        {
            if (CharactorNumberOfOccurence.Any(a => a.Key.Equals(c)))
            {
                count = CharactorNumberOfOccurence.Where(a => a.Key.Equals(c)).Select(a => a.Value).FirstOrDefault() + 1;
                CharactorNumberOfOccurence.Remove(c);
                CharactorNumberOfOccurence.Add(c, count);
            }
            else
            {
                CharactorNumberOfOccurence.Add(c, count);
            }
        }

        foreach (KeyValuePair<char, int> charactor in CharactorNumberOfOccurence)
        {
            if (charactor.Value.Equals(number))
            {
                if (char.IsWhiteSpace(charactor.Key))
                    Console.WriteLine("SPACE");
                else
                    Console.WriteLine(charactor.Key.ToString().ToUpper());
            }
        }
}

With group by it is very simple :)

public static void GetNumberOfTimesACharacterOccuredInAString(string text, int number)
{
            List<char> result = text.GroupBy(x => x).Where(y => y.Count() == number).Select(y => y.Key).ToList();

            foreach (char c in result)
            {
                if (char.IsWhiteSpace(c))
                {
                    Console.WriteLine("SPACE");
                }
                else
                {
                    Console.WriteLine(c.ToString());
                }
            }
 }
        string[] arrpattern = new string[textBox.Text.Length];
        int[] arrcount = new int[textBox.Text.Length];
        int counter=0;
        bool found;
        string pattern="";
        for (int i = 0; i < textBox.Text.Length-Convert.ToInt32(textBox2.Text)+1; i++)
        {
            pattern=textBox.Text.Substring(i,Convert.ToInt32(textBox2.Text));
            if(counter==0)
            {
                arrpattern[0] = pattern;
                arrcount[0] = 1;
                counter++;

            }
            else
            {
                found = false;
                for(int j=0; j<counter;j++)
                {

                    if(arrpattern[j]==pattern)
                    {
                        arrcount[j] = arrcount[j] + 1;
                        found = true;
                        break;
                    }
                }
                if(found==false)
                {
                    arrpattern[counter] = pattern;
                    arrcount[counter] = 1;
                    counter++;
                }

            }
        }
        int max=0;
        for(int i =0; i<counter;i++)
        {
            if(arrcount[i]>max)
            {
                max = arrcount[i];
            }
        }
        textBox3.Text=max.ToString();
        textBox1.Text="";
        for (int i = 0; i < counter; i++)
        {
            if (arrcount[i] == max)
            {
                textBox1.Text += "," + arrpattern[i];
            }
        }
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM