The topic comes from the "point of the sword" in the face question 3: find the duplicate number in the Array.
Title: 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 I don't know how many numbers have been duplicated,
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 the repeating number 2 or 3.
There are a number of workarounds, including array ordering, hash table method, and the Author's recommended rearrangement array method. Here is an example of your own approach, with space for time, the new array to achieve a quick lookup, the practice is to create a new length of the array newarray, the initialization value is 1 , the value of the numbers array is assigned as the subscript of the NewArray and the corresponding value is newarray, where number assigns a value to the newarray, determines whether the newarray corresponds to the subscript is-1, A value of 1 indicates that the NewArray has been assigned the same number, indicating that there are duplicate Numbers. Take the array {2,3,1,0,2,5,3} as an example, create a new array of length 7 { -1,-1,-1,-1,-1,-1,-1}, and then assign the value { -1,-1,2,-1,-1,-1,-1},{-1,-1,2,3,-1,-1,-1},{-1,1,2, 3,-1,-1,-1},{0,1,2,3,-1,-1,-1}, The next time you assign 2 to a new array, it is found that the new array is labeled with 2 as 2, indicating that it was assigned by 2 and that 2 is Duplicated.
#include <cstdio>//parameters://numbers: An array of integers//Length: The lengths of the arrays//duplication: (output) A repeating number in an array//return value://true-the Input is valid and there are duplicate numbers in the array//false-the input is invalid, or there are no duplicate numbers in the array//BOOL Duplicate (int numbers[], int length, int* Duplication)//{// //int* duplication uses a pointer-type parameter to access an object outside the function, and the pointer can access and modify the object pointed to, but the copied pointer is a two different pointer// //we recommend replacing pointers with formal parameters of reference types//if (numbers = = nullptr | | length <= 0)//return false;////for (int i = 0; i < length; ++i)// {//if (numbers[i] < 0 | | numbers[i] > Length-1)//return false;// }////for (int i = 0; i < length; ++i)// {//while (numbers[i]! = I)// {//if (numbers[i] = = Numbers[numbers[i]])// {//*duplication = numbers[i]; //Solution Reference//return true;// }//// //Exchange numbers[i] and numbers[numbers[i]]//int temp = numbers[i];//numbers[i] = numbers[temp];//numbers[temp] = temp;// }// }//return false;//}/*my new method: a new array of length newarray, the initialization value is-1, the value of the numbers array as the NewArray subscript and the corresponding value for the NewArray assignment, where number is assigned to newarray, the judgment NewArray corresponds to whether the subscript is-1, if not 1 means that NewArray has been assigned the same number, indicating that there is a duplicate number exists*/BOOLDuplicateintnumbers[],intLengthint*Duplication) { //filter out NULL pointers and non-conforming inputs if(numbers = = Nullptr | | length <=0) return false; for(inti =0; I < length; ++I) {if(numbers[i] <0|| numbers[i] > length-1) return false; } int* Temparray =New int[length];//Dynamic Arrays for(int* q= temparray;q!= temparray+length;++Q) {//iterating through an array of initialization*q =-1; } for(intj =0; J < length; J + +) { if(temparray[numbers[j]] = =-1) temparray[numbers[j]]=numbers[j]; Else if(temparray[numbers[j]]==Numbers[j]) { //had been assigned one or more times*duplication = numbers[j];//Solution Reference Delete[] temparray; return true; } } Delete[] temparray;//remove pointer to array, not omit square brackets return false;}//==================== test Code ====================BOOLContainsintarray[],intLengthintnumber ) { for(inti =0; I < length; ++I) {//as long as there is a match, that returns True if(array[i] = =number )return true; } return false;}voidTestChar* testname,intnumbers[],intlengthnumbers,intexpected[],intexpectedexpected,BOOLValidargument) {printf ("%s Begins:", testname); //expected[]; Duplicate Results//expectedexpected; Repeat Quantity intduplication;//number of custom function repetitions BOOLValidinput = Duplicate (numbers, lengthnumbers, &duplication); if(validargument = =Validinput) { //whether there are duplicates if(validargument) {if(contains (expected, expectedexpected, duplication)) printf ("passed.\n"); Elseprintf ("failed.\n"); } Elseprintf ("passed.\n"); } Elseprintf ("failed.\n");}//the number that repeats is the smallest number in the arrayvoidtest1 () {intnumbers[] = {2,1,3,1,4 }; intduplications[] = {1 }; Test ("Test1", numbers,sizeof(numbers)/sizeof(int), duplications,sizeof(duplications)/sizeof(int),true);}//the number that repeats is the largest number in the arrayvoidtest2 () {intnumbers[] = {2,4,3,1,4 }; intduplications[] = {4 }; Test ("Test2", numbers,sizeof(numbers)/sizeof(int), duplications,sizeof(duplications)/sizeof(int),true);}//There are multiple duplicate numbers in the arrayvoidtest3 () {intnumbers[] = {2,4,2,1,4 }; intduplications[] = {2,4 }; Test ("Test3", numbers,sizeof(numbers)/sizeof(int), duplications,sizeof(duplications)/sizeof(int),true);}//No duplicate Numbers.voidtest4 () {intnumbers[] = {2,1,3,0,4 }; intduplications[] = {-1};//Not in use in the test functionTest"Test4", numbers,sizeof(numbers)/sizeof(int), duplications,sizeof(duplications)/sizeof(int),false);}//No duplicate Numbers.voidtest5 () {intnumbers[] = {2,1,3,5,4 }; intduplications[] = {-1};//Not in use in the test functionTest"Test5", numbers,sizeof(numbers)/sizeof(int), duplications,sizeof(duplications)/sizeof(int),false);}//Invalid inputvoidTest6 () {int* numbers =nullptr; intduplications[] = {-1};//Not in use in the test functionTest"Test6", numbers,0, duplications,sizeof(duplications)/sizeof(int),false);}voidmain () {test1 (); Test2 (); Test3 (); Test4 (); Test5 (); Test6 (); GetChar ();}
Finds duplicate numbers in an array