```
public int findDuplicate(int[] nums) {
Arrays.sort(nums);
int left = 0, right = nums.length-1;
int mid = 0;
while(left<right){
mid = left+(right-left)/2;
if(mid>nums[mid]-1){
right = mid;
}
else {
left = mid+1;
}
}
return right;
}
```