Problem: In an array of integers except two numbers, the other numbers appear two times. Please write the program to find the two only occurrences of the number. The time complexity is O (n) and the space complexity is O (1). (201,200 degrees)
#include <iostream>using namespace std;int findFirst1 (int num) {int n=0;while ((num&1) ==0&&n<32) { num=num>>1;n++;} return n;} BOOL Is_1 (int num, int count) {Num=num>>count;return (num&1);} void findnumsappearonce (int a[],int length, int &num1, int &num2) {if (length<2) return;int res=0;for (int i=0;i <length;i++) {res^=a[i];} int Count=findfirst1 (RES), for (int i=0;i<length;i++) {if (Is_1 (A[i], count)) {num1^=a[i];} Else{num2^=a[i];}}} int main () {int a[]={1, 2, 7, 8, 1, 7, ten, 5, 8, 10};int n=sizeof (A)/sizeof (int); int num1=0, num2=0; Findnumsappearonce (A, N, NUM1, num2);cout<< "num1=" <<num1;cout<< "num2=" <<num2;return 0;}
To find two distinct integers in an array of integers