Topic:
Array a[n],1 to N-1 this N-1 number is stored in a[n], where a number repeats once. Write a function to find the number that is repeated.
Method one: different or law.
The number of N in the array a[n] is different or the result is different from the result of 1 to N-1, and the resulting value is the request.
- The number of repetitions is a, the remaining N-2 number is different or the result is B.
- n number is different or result is a^a^b
- 1 to N-1 xor or result is a^b
- Due to the different or satisfying commutative law and binding law, and x^x = 0 0^x = X;
- Then there are
- (a^b) ^ (a^a^b) =a^b^b=a
Code:
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> void xor_finddup (int * a,int N) { int i; int result=0; for (i=0;i<n;i++) { result ^= a[i]; } for (i=1;i<n;i++) { result ^= i; } printf ("%d\n", result); } int main (int argc, char* argv[]) { int a[] = {1,2,1,3,4}; Xor_finddup (a,5); return 0; }
Method Two: Mathematical method.
Sums all the items of an array, minus 1 to N-1, which is the number to be evaluated.
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> void xor_finddup (int * a,int N) { int tmp1 = 0; int tmp2 = 0; for (int i=0; i<n-1; ++i) { tmp1+= (i+1); Tmp2+=a[i]; } TMP2+=A[N-1]; int result=tmp2-tmp1; printf ("%d\n", result); } int main (int argc, char* argv[]) { int a[] = {1,2,4,3,4}; Xor_finddup (a,5); return 0; }
For summation, you can define a macro directly based on the formula. #define SUM (x) (x* (x+1)/2)
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define SUM (x) (x* (x+1)/2) void Xor_finddup (int * A,int N) { int tmp1 = SUM (N-1);//note N-1 to parentheses int tmp2 = 0; for (int i=0; i<n; ++i) { tmp2+=a[i]; } int result=tmp2-tmp1; printf ("%d\n", result); } int main (int argc, char* argv[]) { int a[] = {1,2,4,2,3}; Xor_finddup (a,5); return 0; }
Method Three: Flag Array method
Apply for a string of length n-1 and all ' 0 '. Then iterate through the A[n] array, take the value of each array element A[i], place its corresponding string in the corresponding position 1, if it has been placed 1, then the number is the number of repetitions. is implemented using bitmaps. If space complexity is considered, its space O (N)
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define SUM (x) (x* (x+1)/2) void Xor_finddup (int * Arr,int NUM) { int *arrayflag = (int *) malloc (num*sizeof (int)); int i=1; while (I<num) { Arrayflag[i] = false; i++; } for (i=0; i<num; i++) { if (arrayflag[arr[i] = = False) Arrayflag[arr[i]] = true; The sign appears flag else { printf ("%d\n", Arr[i]); return; Returns the value that has occurred }} } int main (int argc, char* argv[]) { int a[] = {1,3,2,4,3}; Xor_finddup (a,5); return 0; }
Method Four: Fixed offset method
A[n], inside is 1 to N-1. The original array A[i] The largest is N-1, if a[i]=k in a certain place, will a[k] add a N, Mark, when a place a[i]=k again, view A[k] can know K has appeared. This method does not have to open an additional memory space for O (N), but restores the array after checking for the weight.
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> void xor_finddup (int * Arr,int NUM) {int temp=0; for (int i=0; i<num; i++) {if (arr[i]>=num) temp=arr[i]-num; This value is repeated because it was added once, else temp=arr[i]; if (arr[temp]<num) {arr[temp]+=num;//Mark} else {printf ("with duplicate%d\n", temp); Return }} printf ("No Duplicates"); return; } void Clear (int *data,int num)//cleanup data {for (int i=0;i<num;i++) {if (data[i]>num) Data[i]-=num; }} int main (int argc, char* argv[]) {int a[] = {2,4,3,4,1}; Xor_finddup (a,5); Clear (a,5); RetUrn 0; }
Method Five: Symbol Sign method
The last method appears after the addition of N, can also appear after the addition of a minus sign, is the symbol of the method.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <time. h> void xor_finddup (int * Arr,int NUM) {int temp=0; for (int i=0; i<num; i++) {if (arr[i]<0) temp=0-arr[i]; This value is repeated because it was added once, else temp=arr[i]; if (arr[temp]>0) {arr[temp]=0-arr[temp];//Mark} else {printf ("with duplicate%d\n", temp); Return }} printf ("No Duplicates"); return; } void Clear (int *data,int num)//cleanup data {for (int i=0;i<num;i++) {if (data[i]< ; 0) Data[i]=0-data[i]; }} int main (int argc, char* argv[]) { int a[] = {3,2,1,4,1}; Xor_finddup (a,5); Clear (a,5); return 0; }
The above method has a limit on the range of values of the array element, and if the value of the element is not in the range of 1 to N-1, the maximum value of the group element can be evaluated first.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <time. h> int do_dup_mal (int arr[], int n, int *pre, int *back) {int MAX =-1; int i = 0; int sameval =-1; *pre = *back =-1; for (int j=0; j<n; J + +) {if (Arr[j] > max) max = arr[j];//Find the largest number in the array} char *arrayflag = new Char[max+1]; if (NULL = = Arrayflag) return-1; memset (arrayflag, 0, max+1); ' i=0 ' = = 0 for (i<n; i++) {if (arrayflag[arr[i]] = = ' + ') arrayflag[arr[i]] = ' \1 ‘; Sameval = Arr[i];//Returns the value that has occurred *back = i; Break }} delete[] Arrayflag; if (I < n) {for (int j=0; j<n; J + +) {if (Sameval = = Arr[j]) { *pre = j; return true; } } } return false; } void Main (int argc, char *argv[]) {int prepos =-1, Backpos =-1; int myarry[11]; Myarry[0] = 1; MYARRY[1] = 3; MYARRY[2] = 3; MYARRY[3] = 4; MYARRY[4] = 5; MYARRY[5] = 22; MYARRY[6] = 7; MYARRY[7] = 13; MYARRY[8] = 9; MYARRY[9] = 2; MYARRY[10] = 12; if (Do_dup_mal (Myarry, one, &prepos, &backpos)) printf ("%d\n", Myarry[prepos]); }
Find out the number of unique duplicates in the array (GO)