First, the topic
All numbers in an array of length n are within 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}, the corresponding output is the first repeating number 2.
Second, the solution
1 Public classSolution {2 //Parameters:3 //numbers:an array of integers4 //length:the length of array numbers5 //duplication: (Output) The duplicated number in the array number,length of duplication array is 1,so using Duplicat Ion[0] =? in implementation;6 //Here duplication-like Pointor-C + +, duplication[0] equal *duplication in C/C + +7 //here to pay special attention ~ return any duplicate one, assignment value duplication[0]8 //Return Value:true If the input is valid, and there be some duplications in the array number9 //otherwise falseTen Public BooleanDuplicateintNumbers[],intLengthint[] duplication) { One Boolean[] k =New Boolean[length]; A for(inti = 0; I < k.length;i++){ - if(K[numbers[i]] = =true){ -Duplication[0] =Numbers[i]; the return true; - } -K[numbers[i]] =true; - } + return false; - } +}
50. Repeated numbers in the array