Goal: each positive integer p should be placed at index p-1; in the end, iterate from index=0 to right, stop at the first index whose value is non-positive; and return the missing integer as index+1.

To achieve this:

- Iterate through the array.
- If current element, 'curr', is positive and its value is not equal to index+1, move it to index+1.
- If there is another positive integer at index+1, continue moving that integer.
- After moving curr to the right location, reset this index to 0.
- In the end, iterate from index 0 to right, stop at first index with non-positive value. return index+1.

```
public int firstMissingPositive(int[] A) {
for (int i = 0; i < A.length; i++) {
if (A[i] > 0 && A[i] != i+1) {
int curr = A[i];
A[i] = 0;
while (curr > 0 && curr-1 < A.length) {
int next = A[curr-1];
A[curr-1] = curr;
if (curr != next) // have to check this, otherwise infinity loop
curr = next; // continue placing next at right index, if it's positive.
else
curr = 0;
}
}
}
// look for first index with non-positive value
for (int i = 0; i < A.length; i++) {
if (A[i] <= 0)
return i+1;
}
return A.length + 1;
}
```