If additional arrays can be used, we can open a new array to count the number of occurrences of each number.
We can use dichotomy to do this question, because n+1 numbers in 1-n must have two numbers that are repeated, so every time we divide by the middle value, then there must be a number of intervals on the left and right. Is greater than its l-mid+1, this greater than is the repeated number
Code
classSolution{
public:intduplicateInArray(vector<int>& nums){
int l=1,r=nums.size()-1;while(l<r){
int mid=l+r>>1;int s=0;for(auto x:nums){
if(x>=l&&x<=mid){
s++;}}if(s>mid-l+1) r = mid;else l = mid +1;}return l;}};