How to make a number appear in an array only once

Source: Internet
Author: User
Tags arrays include printf first row

Topic description

In an integer array, except for two digits, the other numbers appear two times. Please write a program to find out the two numbers that appear only once.

Input

Each test case consists of two lines

The first row contains an integer n that represents the size of the array. 2<=n <= 10^6.

The second row contains n integers, which represent the array elements, and the elements are int.

Output

For each test case, only two numbers appear once in the output array. The number of output from small to large order.

Sample input:

8

2 4 3 6 3 2 5 5

Sample output:

4 6

Train of thought: the principle of XOR or weight has been learned in the blog post, and knowing that if there is only one occurrence of a number, but here are two numbers that appear only once, and we have to find a way to divide him into two sub arrays, each containing a single occurrence of only one digit, and the other numbers appearing two times. The sword refers to the idea of an offer that is ingenious, still different from beginning to end or all the figures, the result is actually two only one occurrence of the number of different or results, we in the difference or the result of the second in the binary is the rightmost 1 bit, since the bit is 1, indicating the difference or two digits corresponding to the bit is definitely different, One must be 1, one is 0, so we can consider dividing these two arrays according to whether this bit is a. So the two numbers that appear only once are separated, but we also want to make sure that the number two times is divided into the same sub array, certainly not two duplicate numbers in two different sub arrays, The result is not correct, it is obvious that the same number of digits on the same bit is the same value, either 1 or 0, so we can also divide the number two times into the same sub array by determining whether the bit is one, and if it is 1, it is divided into a single array, and if 0, is divided into another sub array. This ensures that there is only one occurrence of the number in each of the sub arrays, and that the other numbers appear two times, respectively, to get the numbers that appear only once. Time complexity is O (n).

In addition, after all the elements are different or, in the case of finding the rightmost 1, I use more concise code than the sword refers to the offer, mainly to the following conclusions:

For a digital x,x& (-X), the number that is obtained is to keep the rightmost 1 of X, and the other bits are all 0. Notice here that x is the opposite number,-x=~x+1, where the ~x means to reverse all the bits of x, not to confuse the two.

The following is the AC code:

#include <stdio.h> #include <stdbool.h>/* Returns the lowest digit of Num 1, and everyone else is 0 */int FindFirstBit1 (int num) {  
Both and the resulting number, the NUM rightmost 1 is preserved, the other bits are all set to 0 return num & (-num); }/* To determine whether a particular bit in data is 1, where the specific bits to be judged are determined by res, only one in res is 1, and the other bits are 0, which are returned by the FINDFIRSTBIT1 function, and the bit in data to be judged is the bit of the only 1 in Res.  
/bool IsBit1 (int data,int res) {return (data&res) ==0)? False:true; } void findnumsappearonce (int *arr,int len,int *num1,int *num2) {if (Arr==null | | len<2) RE  
       
    Turn  
    int i;  
    int allxor = 0;  
       
    All XOR or for (i=0;i<len;i++) Allxor ^= arr[i];  
       
    int res = FINDFIRSTBIT1 (allxor);  
    *NUM1 = *num2 = 0;  
        for (i=0;i<len;i++) {if (IsBit1 (arr[i],res)) *num1 ^= Arr[i];  
    else *num2 ^= Arr[i];  
    int main () {static int arr[1000000];  
    int n;  
  while (scanf ("%d", &n)!= EOF) {      int i;  
       
        for (i=0;i<n;i++) scanf ("%d", arr+i);  
        int num1,num2;  
        Findnumsappearonce (ARR,N,&AMP;NUM1,&AMP;NUM2);  
        if (Num1 < num2) printf ("%d%dn", num1,num2);  
    else printf ("%d%dn", NUM2,NUM1);  
return 0; }

/**************************************************************

problem:1351

User:mmc_maodun

Language:c

result:accepted

time:780 ms

memory:4820 kb

****************************************************************/

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.