Skip to content

Commit bee1b5a

Browse files
authored
Create 2965-find-missing-and-repeated-values.js
Solved find-missing-and-repeated-values
1 parent 3a66f08 commit bee1b5a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Brute Force | Hash Set
3+
* Time O(n^2) | Space O(n^2)
4+
* https://leetcode.com/problems/find-missing-and-repeated-values
5+
* @param {number[][]} grid
6+
* @return {number[]}
7+
*/
8+
var findMissingAndRepeatedValues = function(grid) {
9+
10+
const n = grid.length;
11+
const haves = new Set();
12+
const visited = new Set();
13+
let doubleCount = -1;
14+
15+
for (let i = 0; i < n; i++) {
16+
for (let j = 0; j < n; j++) {
17+
haves.add(grid[i][j]);
18+
if (visited.has(grid[i][j])) {
19+
doubleCount = grid[i][j];
20+
}
21+
visited.add(grid[i][j]);
22+
}
23+
}
24+
25+
let missing = -1;
26+
27+
for (let i = 0; i < n**2; i++) {
28+
if (!haves.has(i+1)) {
29+
missing = i+1;
30+
}
31+
}
32+
33+
return [doubleCount, missing];
34+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy