Skip to main content

2965.js

/** ------------------------------------------------------------------------------
*
* 2965. Find Missing and Repeated Values
* Topics: Math
* https://leetcode.com/problems/find-missing-and-repeated-values/?envType=daily-question&envId=2025-03-06
*
------------------------------------------------------------------------------ */
/**
* @param {number[][]} grid
* @return {number[]}
*/
var findMissingAndRepeatedValues = function (grid) {
const n = grid.length
const count = Array(n * n + 1).fill(0)
let repeated = 0
let missing = 0

for (const row of grid) {
for (const num of row) {
count[num]++
if (count[num] === 2) {
repeated = num
}
}
}

for (let i = 1; i <= n * n; ++i) {
if (count[i] === 0) {
missing = i
break
}
}

return [repeated, missing]
}

console.log(
findMissingAndRepeatedValues([
[1, 3],
[2, 2],
]),
)