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.
