LeetCode solutions

3227. Find Missing and Repeated Values

My accepted JavaScript solution to LeetCode problem 3227, Find Missing and Repeated Values, running in 1ms.

  • Difficulty: Easy
  • JavaScript
  • Runtime 1ms
  • Memory 58.6MB

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 1ms, memory 58.6MB, accepted 2025-12-29.

javascript
/**
* @param {number[][]} grid
* @return {number[]}
*/
var findMissingAndRepeatedValues = function(grid) {
    const n = grid.length;
    const count = new Array(n * n + 1).fill(0);
    for (const row of grid) {
        for (const val of row) {
            count[val]++;
        }
    }
    let repeated = 0, missing = 0;
    for (let i = 1; i <= n * n; i++) {
        if (count[i] === 2) repeated = i;
        if (count[i] === 0) missing = i;
    }
    return [repeated, missing];
};

Source