```
class Solution {
public:
int firstMissingPositive(int A[], int n) { //if the array is[1,2,2,2,4],this can't give us 3 right?However the code got an AC
for(int i=0;i<n;i++){
while(A[i]!=i+1&&A[i]<n&&A[i]>0){
if(A[i]==A[A[i]-1]) break;
swap(A[i],A[A[i]-1]);
}
}
for(int i=0;i<n;i++){
if(A[i]!=i+1) return i+1;
}
return n+1;
}
};
```