Your challenge is to write a function/program that takes a matrix of integers m and a number n as input and:
Splits m into n by n chunks
Replaces each chunk with the most common value in that chunk (In case of a tie, any of the tied values is fine).
Outputs the resulting matrix.
Note: You can take either the size of a single chunk, or the number of chunks to a side.
Example:
0 1 0 1
0 0 1 1
0 0 0 0
0 0 1 1,
2
Divide into chunks:
0 1|0 1
0 0|1 1
---+---
0 0|0 0
0 0|1 1
Take the most common value in each sub-matrix
0|1
-+-
0|0
So
0 1
0 0
is the result!
Note: You can assume that there will always be an integer amount of chunks with integer size.
Input will always be square.
Testcases
These are formatted as taking the size of a single chunk.
0 1 2 1 2 1
2 2 1 0 2 2
2 1 2 0 1 0
0 0 1 0 3 2
3 0 2 0 3 1
1 0 3 2 0 1,
3 =>
2 1
0 0
(The 1 is a tie, any of 012 are fine)
0 1 2 1 2 1
2 2 1 0 2 2
2 1 2 0 1 0
0 0 1 0 3 2
3 0 2 0 3 1
1 0 3 2 0 1,
2 =>
2 1 2
0 0 3
0 2 1
(The 3 is a tie, any of 0123 are fine)
1,
1 =>
1
(kinda obvious edgecase)
Scoring
This is code-golf, shortest wins!