public static int MostCommonValue(int[,] arr) {
int[] counts = new int[arr.GetLength(0) * arr.GetLength(1)];
for (int i = 0; i < arr.GetLength(0); i++) {
for (int j = 0; j < arr.GetLength(1); j++) {
if (counts[arr[i, j]] > mostCommonCount) {
mostCommonValue = arr[i, j];
mostCommonCount = counts[arr[i, j]];
In this version, the function uses an array "counts" to keep track of the count of each unique value in the array. It then iterates through each element in the array, updating the count for each element in the array "counts". Finally, it finds the element with the highest count, and returns that element as the most common value in the array.
Note that this function assumes that the elements of the array are