There is an array of text boxes tb, in each of them there are, say, 5 non-repeating numbers (not repeating only for each box, not for all). It is necessary to calculate the most frequently repeated, say, three numbers in all boxes by pressing the button, output these numbers, for example, in one label, and in the second label, show how many times this three numbers are repeated, and give an example for clarity. Five textboxes, each one contains five numbers, separated by a space: 1 2 3 4 5, 1 2 3 6 7, 1 2 3 8 9, 15 16 17 18 19, 15 16 17 20 21. There are two repeating triples of numbers 1- 2-3 and 15-16-17, but 1-2-3 falls more often than the other, more precisely 3 times. I tried to simply give an example. I understand that the task is difficult, but I hope that you will help. The code helped me, but it does not work. Here is the code:
var arrays = new List<int[]>(); arrays.Add(new int[] { 1,2,3,4,5}); arrays.Add(new int[] { 1,2,3,6,7 }); arrays.Add(new int[] { 1,2,3,8,9 }); arrays.Add(new int[] { 15,16,17,18,19 }); arrays.Add(new int[] { 15,16,17,20,21 }); //create dictionary of fragments создать словарь фрагментов var count = new Dictionary<Fragment, int>(); //enumerate all triples перечислить все тройки foreach(var arr in arrays) for(int i=0;i<arr.Length - 3;i++) { //create fragment создать фрагмент var f = new Fragment(arr[i], arr[i+1], arr[i+2]); //get and set count of same fragments получать и устанавливать подсчет одинаковых фрагментов var c = 0; count.TryGetValue(f, out c); count[f] = c + 1; } //find max count найти максимальное количество var best = count.OrderByDescending(p => p.Value).First(); //most frequently чаще всего Console.WriteLine("Тройка чисел: {0} Повторений: {1}", best.Key, best.Value); ....
class Fragment { private int[] items; public Fragment(params int[] items) { this.items = items; } public override int GetHashCode() { var res = items[0]; for (int i = 0; i < items.Length; i++) res ^= items[i]; return res; } public override bool Equals(object obj) { var other = (Fragment)obj; for (int i = 0; i < items.Length; i++) if (items[i] != other.items[i]) return false; return true; } public override string ToString() { return string.Join(",", items.Select(i=>i.ToString()).ToArray()); } } First, the console issued correctly - triples with numbers 1 2 3 and repetitions 3. But then I changed the numbers for others:
var arrays = new List<int[]>(); arrays.Add(new int[] { 1, 10, 15, 20, 41 }); arrays.Add(new int[] { 1, 9, 15, 14, 73 }); arrays.Add(new int[] { 2, 1, 3, 15, 41 }); arrays.Add(new int[] { 66, 15, 41, 55, 1 }); arrays.Add(new int[] { 56, 15, 73, 4, 11 }); There is only one triple of numbers, which is repeated in these five arrays - 1, 15, 41 and it is repeated 3 times, this can be visually checked. The console gave me the result:
Тройка чисел: 1, 10, 15 Повторений: 1 This code does not work at all (((. It looks like it can only compare the first three numbers in each array. Tell me what's wrong with this code. Although I need code for Windows Forms, not for the console. I don’t see any point work with the console, constantly enter a bunch of arrays, etc. It is easier to make an array of Textboxes, to select all the numbers in the boxes at the touch of a button, and output the results to the text of labels.

