-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolutions.java
More file actions
32 lines (28 loc) · 882 Bytes
/
Solutions.java
File metadata and controls
32 lines (28 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
class Solutions {
public int[] findMissingAndRepeatedValues(int[][] grid) {
Set<Integer> seen = new HashSet<>();
int missing = -1;
int repeating = -1;
int n = grid.length * grid[0].length; // total elements
// Find repeating number
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
int num = grid[i][j];
if (seen.contains(num)) {
repeating = num;
} else {
seen.add(num);
}
}
}
// Find missing number
for (int i = 1; i <= n; i++) { // loop inclusive of n
if (!seen.contains(i)) {
missing = i;
break;
}
}
return new int[]{repeating, missing};
}
}