In a group of numbers, only two numbers appear odd times, and all the other numbers are paired, please find out the two numbers

Source: Internet
Author: User

First look at a simple, a group of numbers, only a number of odd times, all the other numbers are paired, find out the number of odd times. For this problem, we just need to have all the numbers and retrograde XOR. Theoretical formula:

A⊕b=b⊕a

A⊕0=a

A⊕b⊕b=a

A⊕ (b⊕c) = (a⊕b) ⊕c

Code:

#include <stdio.h> #include <stdlib.h>int main () {int arr[] = {1, 2, 3, 4, 1, 2, 3};int ret = 0;int len = size Of (arr)/sizeof (arr[0]); for (int i = 0; i < len; i++) {ret ^= arr[i];} printf ("The number of odd occurrences is:>%d\n", ret); System ("pause"); return 0;}

Now look more difficult, a group of numbers, only two of the number of odd times, all the other numbers are in pairs appear, find out the number of odd times. Through the simple example above, we can know that as long as we divide two into two groups, and then use XOR separately, we can get those two numbers.

Now let's see how to divide a group of numbers into two groups with only one number of each group: first of all, we make an XOR of all the numbers, and then we get the two odd times of the two number of XOR, such as {1, 2, 3, 4, 1, 2, 7, 3}, get 7⊙ 4, this number is certainly not 0, We find the rightmost 1 of this number of binary numbers (set the rightmost 1 in the Inter bit), and then find out the inter bits of each element of the array, and determine whether this bit is 1 or 0, 1 for a group, and 0 for a group, so that the group is divided, and then using the example above, You can get the two numbers that appear odd times.

 #include  <stdio.h> #include  <stdlib.h>int main ( ) {int arr[] = { 1, 2, 3, 4, 1, 2, 7, 3 };int  Len = sizeof (arr)  / sizeof (arr[0]); int ret = 0;int inter = 0 ;int reta = 0;int retb = 0;for  (int i = 0; i <  len; i++) {ret ^= arr[i];} /* Find RET's rightmost 1*/inter = ret -  (ret& (ret - 1));for  (int i =  0; i < len; i++) {int a =  (arr[i] >>  (inter - 1 ))  % 2;   //Remove Arr[i] Inter bit if  (a == 0) {reta ^= arr[i];} Else{retb ^= arr[i];}} printf ("Two number of odd occurrences: > %d,%d\n", &NBSP;RETA,&NBSP;RETB); System ("pause"); return 0;} 


In a group of numbers, only two numbers appear odd times, and all the other numbers are paired, please find out the two numbers

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.