Hi there is a two-dimensional array

string[,] Array = new string[9,9]; 

It is necessary to find more than 5 identical symbols on the diagonal.

Closed due to the fact that off-topic participants Qwertiy , aleksandr barakin , Kromster , Sasha Omelchenko , Streletz 19 May '17 at 18:30 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The message contains only the text of the task, in which there is no description of the problem, or the question is purely formal (" how do I do this task ") . To reopen the question, add a description of the specific problem, explain what does not work, what you see the problem. " - aleksandr barakin, Sasha Omelchenko, Streletz
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 9
    what were the difficulties? - Alexey Shimansky
  • What is the diagonal? - Qwertiy
  • @Qwertiy I think if there was a side, it would have been so written. Although ... - Alexey Shimansky
  • @ Alexey Shimansky, I can divide a 9 * 9 square into a bunch of overlapping 5 * 5 squares and each of them will have 2 diagonals: D - Qwertiy
  • @Qwertiy judging by the description of the issue of the CU - it was not meant such a perversion :)) - Alexey Shimansky

1 answer 1

For this action two steps are enough:

  • First: you need to go through the array and put all the numbers from the diagonal into the array.
  • Second: filter data by the condition "more than 5 identical"

So, step number 1:

Let's look at three arrays:

 var matrix2D = [ 1, 2, 3, 4 ]; var matrix3D = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; var matrix4D = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]; 

If you look closely, you can see the logic by which the numbers are located on the diagonal: the row number and the column number are the same. Knowing it is not difficult to make two cycles with counters i and j , and if they match, enter the number in an additional array (let's call it arr2 ).

Step # 2:

At this step, you need to count the number of occurrences of numbers and filter by condition. How to do it? Ways to a lot:

  1. One way is to use LINQ by selecting from the array exactly those numbers that are greater than 5. For sampling, you can use GroupBy - to group values, Where - to filter by condition (> 5)) and Select for the sample itself. All methods can be found in the Documentation.

  2. Another way: to get a dictionary (Dictionary), where the key is a number, and the value is the number of repetitions of this number. Then make a loop, and at iteration check:

    • No such key? Then we enter it in the dictionary and set the counter to 0.
    • Is there such a key? Then increment the counter in value by 1

As a result, getting the desired result.

  1. Another way: come up with yourself))

Choose a way and think ...


Happy End.