```
int findDuplicate(vector<int>& nums) {
int len = nums.size(), index = 0, tmpNum;
while(index < len)
{
if(nums[index] != index + 1)
{
tmpNum = nums[index];
if(nums[tmpNum - 1] != tmpNum)
{
nums[index] = nums[tmpNum - 1];
nums[tmpNum - 1] = tmpNum;
}
else
{
return tmpNum;
}
}
else
{
index ++;
}
}
return -1;
}
```