```
int findDuplicate(vector<int>& nums) {
int l = 1, r = nums.size() - 1, mid, count = 0;
while( l < r ) {
mid = ( l + r ) / 2;
for( int i : nums ) {
if( i <= mid && i >= l) count++;
}
if( count > mid - l + 1) r = mid;
else l = l == mid ? mid + 1 : mid;
count = 0;
}
return l;
}
```