
😎풀이
- 모든 숫자의 빈도를 0으로 초기화
grid 2중 순회
2-1. 각 숫자의 빈도 기록
- 두 번 탐색된 숫자를 정답 배열의 0번째 인자로 설정
- 한 번도 탐색되지 않은 숫자를 정답 배열의 1번째 인자로 설정
- 정답 배열 반환. 정답 배열 반환
function findMissingAndRepeatedValues(grid: number[][]): number[] {
const nums = Array.from({ length: grid.length * grid[0].length }, (_, i) => i + 1)
const frequent = new Map<number, number>()
for(const num of nums) {
frequent.set(num, 0)
}
for(const row of grid) {
for(const col of row) {
frequent.set(col, (frequent.get(col) ?? 0) + 1)
}
}
let ans = [0, 0]
for(const [key, value] of frequent) {
switch(value) {
case 2:
ans[0] = key
break
case 0:
ans[1] = key
break
}
}
return ans
};