# 59. Summary: N (n = 1, 2, 3) numbers that only appear once in the array [find N numbers which appear only once in array]

Source: Internet
Author: User

Http://www.cnblogs.com/hellogiser/p/find-n-numbers-which-appear-only-once-in-array.html

Question]

Three numbers a, B, and c appear only once in an array, and the other numbers appear twice. Find three numbers that appear only once.

Analysis]

This is a novelBitwise operation. Previous blog posts34. two numbers that appear only once in the array [Find two numbers which appear once]Analysis of N = 1 and N = 2.

(1). N = 1, the result of all numbers in the array is.

(2 ). when N = 2, all numbers in the array are exclusive or the result is equal to a ^ B. The array is divided into two groups based on the position where the last 1 in the binary of a ^ B; returns a and B if the numbers in each group are different or different.

(3) When. N = 3, all numbers in the array are exclusive or the result is equal to a ^ B ^ c. How can we differentiate them? If we can find a number that appears only once, the remaining two digits that appear only once can be converted to N = 2.

The specific ideas are as follows:

(1). f (x) = x & (-x) The result is the position of the last 1 of x.

(2). x = a ^ B ^ c.

(3 ). flag = f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) the result must be 1, because f (m) ^ f (n) the result is 0 or 2.

(4 ). f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) where the m-bit is 1, then x ^ a, x ^ B, x ^ c must have 1 or 3 m-th digits as 1.

(5). We can use the reverse identification method to obtain that x ^ a, x ^ B, and x ^ c have only one m-bit as 1.

For example, data = {1, 2, 4, 4, 5, 5, 6}

X = a ^ B ^ c = 1 ^ 2 ^ 3 = 000

X ^ a = 001, x ^ B = 010, x ^ c = 011

F (x ^ a) = 001, f (x ^ B) = 010, f (x ^ c) = 001

Flag = f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) = 010, flag = f (flag) = 010

F (x ^ B) = flag

First = B = 1

Second = 3, third = 1

The complete code is as follows:

Code]

C ++ Code
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 // 58_FindNumbersAppearOnce.cpp: Defines the entry point for the console application.///*Version: 1.0Author: hellogiserBlog: http://www.cnblogs.com/hellogiserDate: 2014/5/27*/# Include "stdafx. h"// Find number which appear onceVoid Find1NumberAppearOnce (int data [], int length, int & num){If (NULL = data | length <1)Return;// Get the exclusive or result of array//Int xor = 0;For (int I = 0; I 00000010Unsigned int GetLast1Bit (int n){Return n & (-n );}// Find 2 numbers which appear onceVoid Find2NumbersAppearOnce (int data [], int length, int & num1, int & num2){If (NULL = data | length <2)Return;// Get the exclusive or result of array// A ^ BInt xor = 0;For (int I = 0; I

[Reference]

Http://www.cnblogs.com/hellogiser/p/3738909.html

Http://zhedahht.blog.163.com/blog/static/25411174201283084246412/

Http://www.cnblogs.com/youxin/p/3349834.html

Http://www.cnblogs.com/kedebug/archive/2012/12/22/2829013.html

Http://www.cnblogs.com/hellogiser/p/find-n-numbers-which-appear-only-once-in-array.html

Related Keywords:
Related Article

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.