Only one and two digits in an array appear once, and the remaining pairs appear

Source: Internet
Author: User

A number appears once #include<iostream>using namespace std;int once (Int* a, int num) {  int ret = 0; for  (int i = 0; i < num; ++i )  {  ret ^= a[i]; } return ret;} Int main () { int array[5] = { 1, 2, 2, 3, 3 }; int  sum = once (array, 5); cout << sum << endl;  return 0;} Two appear once #include <iostream>using namespace std;int two (Int num) { int  get = 0; while  (num)  {  if  (num % 2 == 1)// Returns the number of digits of the first one that appears in the binary   {   return get;  }  get++;  num  /= 2; } return -1;} Void find (int*a, int ret, int *p, int *q) { int find = 0 ; int pos = 0; for  (int i = 0; i < ret; ++i )  {  find ^= a[i]; } pos = two (Find); for  (int i  = 0; i < ret; ++i)  {  if  (1 &  (a[i]  >> pos))   {   *p ^= a[i];  }  else   {   *q ^= a[i];  } }}int main () { int a[8] =  { 1, 1, 2, 5, 3, 3, 4, 5 }; int ret =  sizeof (a)  / sizeof (a[0]); int num1 = 0; int num2 = 0;  Find (A,&NBSP;RET,&NBSP;&AMP;NUM1,&NBSP;&AMP;NUM2); cout << num1 <<  ","  << num2 << endl; return 0;}


Only one and two digits in an array appear once, and the remaining pairs appear

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.