The title describes all the numbers in an array of length n in the range of 0 to n-1. Some of the numbers in the array are duplicates, but it is not known that several numbers are duplicates. I don't know how many times each number repeats. Please find any duplicate numbers in the array. For example, if you enter an array of length 7 {2,3,1,0,2,5,3}, then the corresponding output is a repeating number of 2 or 3.
1 classSolution {2 Public:3 //Parameters:4 //numbers:an array of integers5 //length:the length of array numbers6 //duplication: (Output) The duplicated number in the array number7 //Return Value:true If the input is valid, and there be some duplications in the array number8 //otherwise false9 BOOLDuplicateintNumbers[],intLengthint*duplication) { Ten for(intI=0; i<length;i++){ One intj=numbers[i]%length; Anumbers[j]+=length; - if(Numbers[j]>= (2*length)) { -*duplication = numbers[j]%length; the return true; - } - } - return false; + } -};
Interview 51 Repeating numbers in the array