I don't quite understand why don't you just sort the array, and then traverse it. Could someone explain why you **prefer to swap each element to its index position**? Thanks a lot!

```
public class Solution {
public int firstMissingPositive(int[] nums) {
if (nums.length==0){
return 1;//will save much time than just sort(nums);
}
Arrays.sort(nums);
int counter=0;
for(int i=0;i<nums.length;i++){
if(nums[i] == counter+1) counter++;
if(nums[i] <= counter) continue;
else break;
}
return counter+1;
}
}
```