From bee1b5ab57a538e19ed99baa2ac34e50e5eb482c Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Fri, 7 Mar 2025 01:11:01 +0530 Subject: [PATCH] Create 2965-find-missing-and-repeated-values.js Solved find-missing-and-repeated-values --- .../2965-find-missing-and-repeated-values.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 javascript/2965-find-missing-and-repeated-values.js 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]; +};