//There are 101 numbers, of which 50 numbers appear two times, one number appears only once, and the number that appears once#include <stdio.h>#include<stdlib.h>intMain () {inta[ One] = {0}; inti; intn =0; printf ("Please input the arr:"); for(i =0; I < One; i++) {scanf ("%d",&A[i]); } for(i =0; I < One; i++) {n^=A[i]; } printf ("appear The one time number is%d", N); System ("Pause"); return 0;
From start to finish or once, the last number you get is the number of odd occurrences. This is because the XOR has a magical nature: two times different or the same number, the result is unchanged. The algorithm is clearly correct, considering that the XOR operation satisfies the commutative law, which is the same as the first or the other or both.
Give you the number of n, of which there is only one number has an odd number of times, the rest of the number has appeared even several times. A linear time constant space is used to find out the number of odd occurrences.