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}, then the corresponding output is a repeating number of 2 or 3.
ImportJava.util.*; Public classSolution {//Parameters://numbers:an array of integers//length:the length of array numbers//duplication: (Output) The duplicated number in the array number,length of duplication array is 1,so using Duplicat Ion[0] =? in implementation; //Here duplication-like Pointor-C + +, duplication[0] equal *duplication in C/C + +//here to pay special attention ~ return any duplicate one, assignment value duplication[0]//Return Value:true If the input is valid, and there be some duplications in the array number//otherwise false Public BooleanDuplicateintNumbers[],intLengthint[] duplication) { if(Numbers = =NULL|| Length <= 0) return false; Set<Integer> set =NewHashset<integer>(); for(inti=0; i<length; i++) { if(!Set.add (Numbers[i])) {duplication[0] =Numbers[i]; return true; } } return false; }}
Repeated numbers in the array