To find two distinct integers in an array of integers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.