Start learning free

LESSON 257 OF 671 · NESTED LOOPS OVER GRIDS

Count in a Grid in JavaScript: Matching Cells

Practice Read the full explanation

Given a grid of integers grid and an integer target, return how many cells of the grid are equal to target.

A grid is an array of rows, and each row is an array of integers.

 

Example 1:

Input: grid = [
  [1, 2, 3],
  [4, 2, 6],
  [2, 8, 2]
], target = 2
Output: 4
Explanation: 2 appears once in the first row, once in the second row and twice in the third row: 1 + 1 + 2 = 4.

Example 2:

Input: grid = [
  [-1, 5],
  [5, -1]
], target = -1
Output: 2
Explanation: The two cells with -1 are in different rows, and each one counts.

Example 3:

Input: grid = [
  [7, 8, 9]
], target = 4
Output: 0
Explanation: No cell is equal to 4.

 

Constraints:

  • grid has at least 1 row and 1 column, and every row has the same number of values.
  • Every value and target is an integer between -1000 and 1000; they can be negative.
STEP 1 OF 5
    Ctrl + Enter to check

    JAVASCRIPT · NESTED LOOPS OVER GRIDS

    Count In Grid in JavaScript, explained

    Problem overview

    The method countInGrid(grid, target) receives a grid of integers, an array of rows where each row is an array of integers, and a number target. It returns how many cells are equal to target.

    For the grid [[1, 2, 3], [4, 2, 6], [2, 8, 2]] and target = 2, the value appears once in the first row, once in the second and twice in the third, so the answer is 4. For [[-1, 5], [5, -1]] and target = -1 the answer is 2: the two cells are in different rows, and each counts. For [[7, 8, 9]] and target = 4 no cell matches and the answer is 0.

    Approach

    This is the counting pattern, applied to a grid. Start a counter at 0, visit every cell, and add 1 whenever a cell equals the target. A grid needs two loops: the outer one visits the rows and the inner one visits the values in the current row. Together they reach every cell exactly once, so every match is counted exactly once.

    Nothing is skipped and nothing is visited twice, so there is no need to track where a match was found. Only the count matters.

    Step by step

    1. Declare count with let and start it at 0. It must be let, not const, because it goes up inside the loops.
    2. Loop over the rows. for (let row of grid) picks one row at a time, from the top row down. Each row is an array of numbers.
    3. Loop over the row's values. for (let value of row) goes through the whole row before the outer loop moves on.
    4. Compare and count. If value === target, count++ adds 1. Other cells are skipped.
    5. Return count after both loops, so every cell is checked first.

    The solution in JavaScript

    class Solution {
        countInGrid(grid, target) {
            let count = 0;
            for (let row of grid) {
                for (let value of row) {
                    if (value === target) {
                        count++;
                    }
                }
            }
            return count;
        }
    }
    

    Details worth knowing:

    • === compares without converting. It is true only when both sides have the same type and value. 2 === 2 is true, and "2" === 2 is false. The loose == would say "2" == 2 is true, which is why === is the usual choice.
    • count++ is short for count = count + 1.
    • No index is needed. The function only asks how many cells match, not where they are. When the position matters, an index loop is the tool, as in finding the first match.

    Complexity

    Time: O(r × c) for r rows and c columns, because each cell is compared once.

    Space: O(1). Only one counter and the loop variables are used.

    Edge cases and common mistakes

    • No match. The counter never changes and the function returns 0.
    • Negative targets. -1 is compared like any value. [[-1, 5], [5, -1]] with target = -1 gives 2.
    • Stopping at the first match. Returning inside the loop would give 1 at most. This problem counts all matches.
    • Returning inside the loops. Put return count after both loops close.
    • Using = instead of ===. if (value = target) assigns instead of comparing, which overwrites value and makes the test true for every cell unless target is 0.
    • Counting rows instead of cells. A row with the target twice, like the third row of the first example, adds 2, not 1.

    Testing

    • grid = [[1, 2, 3], [4, 2, 6], [2, 8, 2]], target = 2 gives 4.
    • grid = [[-1, 5], [5, -1]], target = -1 gives 2.
    • grid = [[7, 8, 9]], target = 4 gives 0.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free