In C language programming, only two numbers appear in one array, and all other numbers appear twice.
In C language programming, only two numbers appear in one array, and all other numbers appear twice.
Code Implementation (Environment: Visual Studio 2017)
// Only two numbers appear in an array once, and all other numbers appear twice. // Find the two numbers and program them. # Include
# Include
Void find (int a [], int sz) {int I = 0; int num1 = 0; int num2 = 0; int num = 0; int flag = 0; for (I = 0; I <sz; I ++) {num = num ^ a [I] ;}for (I = 0; I <32; I ++) {if (num> I) & 1 )! = 1) // returns the first position of 1 in the binary number of this digit after the occurrence of an exception or {flag ++;} else break;} for (I = 0; I <sz; I ++) {if (a [I]> flag) & 1) = 1) // divide it into two groups num1 ^ = a [I]; else num2 ^ = a [I];} printf ("% d \ n", num1, num2);} int main () {int a [] = {1, 2, 2, 6, 6, 4, 4, 5, 8, 5}; int sz = sizeof (a)/sizeof (a [0]); find (, sz); system ("pause"); return 0 ;}