I have a search code for just the number of identical numbers in active textboxes at the press of a button and outputting this amount in label2 : C #

  private void button1_Click(object sender, EventArgs e) { foreach (TextBox tb in Controls.OfType<TextBox>().Where(x => x.Enabled == true)) { label2.Text = Controls.OfType<TextBox>().SelectMany(x => x.Text.Split(" ;:,.-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)).GroupBy(x => x).Count(x => x.Count() > 1).ToString(); } } 

Tell me how to remake this code so that it doesn’t compare all active textboxes at once, but each with the other, for example, the first with the second, the first with the third, and so on, then returns to the second and compares it with the following, and so Further. This is the first. The second is that after the textboxes are compared with each other, the code displays the minimum number of identical numbers in the label text (even wherever), even 0 , that is, even if there are no matches at all. And after all this, I would like to somehow reveal in which textboxes a minimum of duplicate numbers was found, for example, to change their background colors ( BackColor ) for others.

A little explanation to all of this. In code, tb is an array of several text boxes. In each box, all numbers are different and are not repeated, they are entered by the user, not randomly, and separated by spaces. And for clarity, I will give a simple example with three textboxes. In the first there are numbers 10 15 20 , in the second 10 11 25 , in the third there are 30 11 25 . Here it is clear that the first has one coincidence with the second, the second has two coincidences with the third, and the first with the third does not. As a result, min. The number of identical numbers is 0 , and this number is inserted into the text of the label, while the first and third text boxes change their background color.

Yes, and also, if it is not difficult, how can I do that by pressing another button to compare text boxes, not everyone with each, but only the first with all the others? For example, if the textbox 3, then compare the first with the second and with the third, and the second with the third is not necessary. Well, again, after all this, bring out the minimum and change the color of the backgrounds. Thanks a lot in advance.

  • Make a short and minimally understandable condition, for example, посчитать количество чисел, присутсующих во всех массивах сразу . - pavel

1 answer 1

I will give an example on arrays of integers. How to get them and TextBox.Text lines you already have an example in your code.

Suppose we have a list of arrays List<int[]> arrays;

Tell me how to remake this code so that it doesn’t compare all active textboxes at once, but each with the other, for example, the first with the second, the first with the third, and so on, then returns to the second and compares it with the following, and so Further.

simple re-search algorithm.

 var min = a.Max(p => p.Length) + 1; var index1 = -1; var index2 = -1; for(int i = 0; i < arrays.Count - 1; i++) for (int j = i+1; j < arrays.Count; j++) { var t = arrays[i].Intersect(arrays[j]); if(min > t) { min = t; index1 = i; index2 = j; } } 

at the output we get a minimum of possible intersections and two array indices giving this minimal intersection. The complexity of the order is O (N ^ 3) with regard to the search for intersections.

How can I do that by clicking on another button to compare text boxes, not everyone with each, but only the first with all the others?

in the same way, only instead of an external cycle, in which i changes, fix its value on the required index and start with j = 0 . The order complexity is O (N ^ 2) with regard to the search for intersections.

it is necessary that after comparing textboxes with each other, the code should contain the minimum number of identical numbers in the label text (even wherever), even 0, that is, even if there are no matches at all. And after all this, I would like to somehow reveal in which textboxes a minimum of duplicate numbers was found, for example, to change their background colors (BackColor) for others.

Now you know both the value of the minimum, which is left to simply enter into the desired Label , and the indices of the arrays forming the desired pair. If the array indices correspond to TextBox indices, it is not difficult to ensure using the loop from your example, then we also know the TextBox indices that need to be repainted.