The character that appears only once in the array.

Source: Internet
Author: User
Description:
In an integer array, all numbers except two appear twice. Write a program to find the numbers that appear only once.
Input:
Each test case contains two rows: the first row contains an integer N, indicating the array size. 2 <= n <= 10 ^ 6. The second row contains N integers, indicating the array elements, all of which are Int.
Output:
For each test case, only two numbers appear in the output array. The order of output numbers from small to large.
Sample input:
82 4 3 6 3 2 5 5
Sample output:
4 6
Ideas:
Set a and B to appear only once.
1. If there is only one number in the array, the other number appears twice. We can find this number by the difference or nature. Because of the difference or such nature, any number is different or it is always 0, 0, or any number is equal to any number. So we can Traverse the array, one by one or the other, and the last one is the number that appears only once. All other numbers are offset.
2. Now we aim to separate the two numbers that appear only once and put them in two sets, so that we can use the conclusion 1 to do this.
3. We still start from an exclusive or end to get the result of the two numbers that appear only once in the array (a ^ B). The result must not be 0. We can From the right to the left, find the first digit of a ^ B as 1 (Binary), which indicates the first bit of A and B., We divide the array into two parts according to whether this bit is 1, then each part only has one number.

Code:

/* The number that appears only once in the array by rowandjj2014/8/14 */# include <stdio. h> # include <stdlib. h>/*** find the first digit in the binary value of the number xorval. **/INT firstbitis1 (INT xorval) {int Index = 0; while (xorval & 1) = 0) & (index <8 * sizeof (INT) // The operator priority cannot be exceeded, add a few more brackets {index ++; xorval = xorval> 1;} Return Index;}/*** to judge the second index bit in data (Binary) whether it is 1 */bool isbit1 (INT data, int index) {DATA = data> index; return data & 1 ;} /* ** data target array * Len array length * num1 a number that appears only once * Num2 another number that appears only once */bool findnumberappearonce (INT data [], int Len, int * num1, int * num2) {If (Data = NULL | Len <2) {return false;} int I; int xorval = 0; for (I = 0; I <Len; I ++) {xorval ^ = data [I];} int indexof1 = firstbitis1 (xorval); * num1 = * num2 = 0; // be sure to assign the value 0 for (I = 0; I <Len; I ++) {If (isbit1 (data [I], indexof1) {* num1 ^ = data [I];} else {* num2 ^ = data [I] ;}} return true ;}int main () {int N; while (scanf ("% d ",& N )! = EOF) {If (n <= 1) {continue;} int * arr = (int *) malloc (sizeof (INT) * n); If (! ARR) {exit (-1) ;}int I; for (I = 0; I <n; I ++) {scanf ("% d ", arr + I);} int num1, num2; If (findnumberappearonce (ARR, N, & num1, & num2) {If (num1 <num2) {printf ("% d \ n", num1, num2);} else {printf ("% d \ n", num2, num1 );}} free (ARR);} return 0 ;}

The above code can be further optimized, with the following nature: A & (-a) retains the rightmost 1 in the binary form of A, and the other positions are 0. For example, 10 (1010) &-10 (0110) = 0010.Code:
/* The number that appears only once in the array */# include <stdio. h> # include <stdlib. h> bool findfirstappearoncenum (INT data [], int Len, int * num1, int * num2) {If (Data = NULL | Len <= 1) {return false ;} int I; int xorval = 0; for (I = 0; I <Len; I ++) {xorval ^ = data [I];} * num1 = * num2 = 0; // be sure to assign the value 0 int temp = xorval & (-xorval); // result: the rightmost 1 in xorval is reserved with the other bits 0for (I = 0; I <Len; I ++) // traverses the array and divides the array into two parts. Each part only has one digit and appears once {If (data [I] & temp) // One of the bits in temp is 1, and the other bits are 0. The preceding data [I] can separate two numbers that appear only once {* num1 ^ = data [I];} else {* num2 ^ = data [I];} return true;} int main () {int N; while (scanf ("% d", & N )! = EOF) {If (n <= 1) {continue;} int * arr = (int *) malloc (sizeof (INT) * n); If (! ARR) {exit (-1) ;}int I; for (I = 0; I <n; I ++) {scanf ("% d ", arr + I);} int num1, num2; If (findfirstappearoncenum (ARR, N, & num1, & num2) {If (num1 <num2) {printf ("% d \ n", num1, num2);} else {printf ("% d \ n", num2, num1 );}} free (ARR);} return 0 ;}





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.