There are several textboxes on the form, combined into an array of tb, in each of them numbers are entered separated by a space. It is impossible to find the most frequently repeated number and number of repetitions in all textboxes at once. Here is my code, here the counting takes place at the touch of a button:

C #

private void Searsh_Click(object sender, EventArgs e) { foreach (TextBox tb in Controls.OfType<TextBox>().Where(x => x.Enabled == true)) // Проверяет, активны ли текстбоксы { var sorted = tb.Text.GroupBy(x => x).Select(g => new { Value = g.Key, Count = g.Count() }).OrderByDescending(x => x.Count); var odno = sorted.First(); Chisla.Text = odno.Value.ToString(); Povt.Text = odno.Count.ToString(); } } 

sorted - collection of repeating tb numbers - array of textboxes Value - often repeating number Count - number of repetitions Chicla - label, which shows the most frequently repeated number Povt - label, to which the number of repetitions is transmitted

Yes, and I need more to eliminate the error (all of a sudden there are no identical numbers). Well, if there are several such numbers, sort them by the number of repetitions and view them. There is a way, but not sure that it is correct: Code:

 if (sorted.Length >= 2) { var dva = sorted.Skip(1).Take(1); // Пропустить первое число и выбрать второе Chisla.Text = dva.First().Key; Povt.Text = dva.First().Count.ToString(); } 

Thanks in advance for your help.

    1 answer 1

      private void button1_Click(object sender, EventArgs e) { // Словарик с числами и их количеством Dictionary<int, int> repeats = new Dictionary<int, int>(); // Флаг, определяющий есть ли хоть один повтор bool isRepeat = false; // Пробегаем по всем полям foreach (TextBox tb in Controls.OfType<TextBox>().Where(t => t.Enabled == true)) { var numbers = tb.Text.Split(' '); // Пробегаем по всем числам foreach (var numb in numbers.Select(int.Parse)) { if (repeats.ContainsKey(numb)) { repeats[numb] += 1; isRepeat = true; } else { repeats[numb] = 1; } } } if (!isRepeat) { MessageBox.Show("Повторов нет!"); return; } listBox1.Items.Add("Число\tПовторов"); foreach (var item in repeats.OrderByDescending(v => v.Value)) { listBox1.Items.Add(string.Format("{0}\t{1}", item.Key, item.Value)); } } 

    enter image description here