Problem overview
The function countInGrid receives a grid, grid of type [][]int, and an integer target. It returns how many cells of the grid are equal to target.
For the grid [[1, 2, 3], [4, 2, 6], [2, 8, 2]] and target = 2 the answer is 4: one 2 in the first row, one in the second and two in the third. For [[-1, 5], [5, -1]] and target = -1 the answer is 2, because the two cells are in different rows and each one counts. For [[7, 8, 9]] and target = 4 the answer is 0.
Approach: visit every cell and count the matches
This is the counting pattern applied to two dimensions. Keep a counter that starts at 0. Visit every cell with two nested loops, the outer one over rows and the inner one over the values in the current row. Whenever a value equals target, add 1. After the loops, the counter holds the number of matches.
No cell needs to be remembered or skipped, and the position of a match does not matter, only that it exists. That is why range with the index dropped is enough.
Step by step
- Create
countwith:=, starting at 0. - Loop over the rows:
for _, row := range grid. A[][]intis a slice of rows, so eachrowis a[]int. - Loop over the row's values:
for _, value := range row. The inner loop finishes a whole row before the outer loop moves on, so every cell is visited exactly once. - Compare and count: if
value == target, runcount++. Other values are skipped. - Return
countafter both loops, so every cell has been checked first.
The solution in Go
func countInGrid(grid [][]int, target int) int {
count := 0
for _, row := range grid {
for _, value := range row {
if value == target {
count++
}
}
}
return count
}
Go details:
==compares values. For twointvalues,value == targetis true when they are the same number. A single=is assignment, and using it in anifdoes not compile.count++is a statement. It adds 1 tocount. Go has no++countand doesn't letcount++be used inside a larger expression.- Parameter types. The signature
grid [][]int, target intreads as "a slice of slices of int, and an int". Both are passed in directly, and the function doesn't change the grid. - No nested
iffor rows. Theifsits inside the inner loop only, so it runs once per cell.
Edge cases and common mistakes
- No match. If the loops finish and nothing was counted, the function returns 0, as for
[[7, 8, 9]]with target 4. - Negative targets.
-1is compared like any other integer. In[[-1, 5], [5, -1]]both -1 cells are counted. - Several matches in one row. The third row of the first example has two 2s, and both are counted. Stopping at the first match in a row would give 3 instead of 4.
- Returning inside the loops. A
return countin the loop would stop after the first cell, so the function would give a count for only a part of the grid. - Counting rows instead of cells. Adding 1 once per row that contains the target would also give 3 for the first example. The lesson counts cells.
Testing
[[1, 2, 3], [4, 2, 6], [2, 8, 2]]withtarget = 2gives4.[[-1, 5], [5, -1]]withtarget = -1gives2.[[7, 8, 9]]withtarget = 4gives0.
