We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 27ce049 commit 4e645a3Copy full SHA for 4e645a3
2501-3000/2965. Find Missing and Repeated Values.swift
@@ -0,0 +1,33 @@
1
+class Solution {
2
+
3
+ // Solution by Sergey Leschev
4
+ // 2965. Find Missing and Repeated Values
5
6
+ // Time complexity: O(n * n)
7
+ // Space complexity: O(n * n)
8
9
+ func findMissingAndRepeatedValues(_ grid: [[Int]]) -> [Int] {
10
+ var freq = [Int: Int]()
11
+ let n = grid.count
12
13
+ for i in 0..<n {
14
+ for j in 0..<n {
15
+ freq[grid[i][j], default: 0] += 1
16
+ }
17
18
19
+ var repeated = 0
20
+ var missing = 0
21
22
+ for i in 1...(n * n) {
23
+ if freq[i] == 2 {
24
+ repeated = i
25
26
+ if freq[i] == nil {
27
+ missing = i
28
29
30
31
+ return [repeated, missing]
32
33
+}
0 commit comments