문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
1부터 n까지의 모든 숫자가 포함되어 있는 정수 집합 s가 있다. 하지만 어떤 오류로 인해 집합 s에 있는 숫자 중 하나가 중복되어 다른 숫자 하나가 사라지는 문제가 발생했다.
오류 발생 후 해당 데이터 세트의 상태를 나타내는 정수 배열 nums가 주어진다.
두 번 나타나는 숫자와 나타나지 않는 숫자를 찾아 배열 형태로 반환해라.
#1
Input: nums = [1, 2, 2, 4]
Output: [2, 3]
#2
Input: nums = [1, 1]
Output: [1, 2]
class Solution {
public int[] findErrorNums(int[] nums) {
int n = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 1; i <= n; i++){
map.put(i, 1);
}
for(int num : nums){
map.put(num, map.get(num) - 1);
}
int duplicate = 0;
int missing = 0;
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
if(entry.getValue() == -1){
duplicate = entry.getKey();
}
if(entry.getValue() == 1){
missing = entry.getKey();
}
}
return new int[]{duplicate, missing};
}
}