forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetMismatch.java
43 lines (37 loc) · 949 Bytes
/
SetMismatch.java
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
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public int[] findErrorNums(int[] nums) {
int[] ans = new int[2];
for(int i=0;i<nums.length;i++){
int index = Math.abs(nums[i])-1;
if(nums[index] < 0){
ans[0] = index+1;
} else{
nums[index] = -nums[index];
}
}
for(int i=0;i<nums.length;i++){
if(nums[i]>0){
ans[1] = i+1;
}
}
return ans;
}
public int[] findErrorNums(int[] nums) {
int[] ans = new int[2];
Set<Integer> set = new HashSet<>();
for(int el : nums){
if(set.contains(el)){
ans[0] = el;
} else{
set.add(el);
}
}
for(int i=1;i<=nums.length;i++){
if(!set.contains(i)){
ans[1] = i;
break;
}
}
return ans;
}
}