diff --git a/javascript/2965-find-missing-and-repeated-values.js b/javascript/2965-find-missing-and-repeated-values.js new file mode 100644 index 000000000..0c2eaa35d --- /dev/null +++ b/javascript/2965-find-missing-and-repeated-values.js @@ -0,0 +1,34 @@ +/** + * Brute Force | Hash Set + * Time O(n^2) | Space O(n^2) + * https://leetcode.com/problems/find-missing-and-repeated-values + * @param {number[][]} grid + * @return {number[]} + */ +var findMissingAndRepeatedValues = function(grid) { + + const n = grid.length; + const haves = new Set(); + const visited = new Set(); + let doubleCount = -1; + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + haves.add(grid[i][j]); + if (visited.has(grid[i][j])) { + doubleCount = grid[i][j]; + } + visited.add(grid[i][j]); + } + } + + let missing = -1; + + for (let i = 0; i < n**2; i++) { + if (!haves.has(i+1)) { + missing = i+1; + } + } + + return [doubleCount, missing]; +};