Give you the number of n, of which there is only one number has an odd number of times, the rest of the number has appeared even several times. A linear time constant space is used to find out the number of odd occurrences.

Source: Internet
Author: User

//There are 101 numbers, of which 50 numbers appear two times, one number appears only once, and the number that appears once#include <stdio.h>#include<stdlib.h>intMain () {inta[ One] = {0}; inti; intn =0; printf ("Please input the arr:");  for(i =0; I < One; i++) {scanf ("%d",&A[i]); }     for(i =0; I < One; i++) {n^=A[i]; } printf ("appear The one time number is%d", N); System ("Pause"); return 0;

From start to finish or once, the last number you get is the number of odd occurrences. This is because the XOR has a magical nature: two times different or the same number, the result is unchanged. The algorithm is clearly correct, considering that the XOR operation satisfies the commutative law, which is the same as the first or the other or both.

Give you the number of n, of which there is only one number has an odd number of times, the rest of the number has appeared even several times. A linear time constant space is used to find out the number of odd occurrences.

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.