[Sword refers to offer] unique or deduplicated

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/ns_code/article/details/27568975


This article does not have code and introduces pure theoretical ideas.

XOR is a binary-based bitwise operation that is expressed by the symbol XOR or ^. The algorithm is to take 0 for each binary bit of the number on both sides of the operator, and 1 for the same value. It differs from a Boolean operation in that when both sides of an operator are 1, the result of a Boolean operation is 1, and the result of an exclusive or operation is 0.

Exclusive nature:

1. Exchange Law: a ^ B = B ^;

2. Combination Law: (a ^ B) ^ c = a ^ (B ^ c );

3. For any a: a ^ a = 0, a ^ 0 = a, a ^ (-1) = ~ A.

After learning about the above, it is very important to take a look at this. The subsequent procedures will use this conclusion:

For any a, there are a ^ B ^ c ^ d ^ a ^ k = B ^ c ^ d ^ k ^ (a ^) = B ^ c ^ d ^ k ^ 0 = B ^ c ^ d ^ k. That is to say, if there are multiple numbers that are different or have duplicates, then, no matter whether the number of duplicates is adjacent or not, the number of duplicates can be deleted based on the exclusive or nature. Specifically, if an even number of repetitions occurs, if an odd number of times is repeated, the same or is retained.

Here are two questions:

1. 1-1001 are placed in an array containing elements. Only one unique element value is repeated, and only one other element is displayed. Each array element can only be accessed once. design an algorithm to find it. Can you design an algorithm to implement it without supporting the storage space?

Of course, this question can be done in the most intuitive way, adding all the numbers, minus 1 + 2 + 3 +... + The sum of 1000 is the number of duplicates. This method is easy to understand and highly efficient. No auxiliary space is required. If the range is not 1000, A larger number may overflow.

We consider using an exception or operation to solve this problem. Now the problem is the number that needs to be repeated. Let's assume that the number is n. If we can find a way to make all the numbers except n in 1-different or twice, if the number n is an exclusive or one time, all numbers out of n from 1 to can be deleted, so that only n is left. First, we will record all numbers as T, and we can get the following:

T = 1 ^ 2 ^ 3 ^ 4... ^ n... ^ n... ^ 1000 = 1 ^ 2 ^ 3... ^ 1000 (n is not included in the result)

Then let's let all the numbers (only one n) between T and 1-be exclusive or, then we can get the repeated number n. As follows:

T ^ (a ^ 2 ^ 3 ^ 4... ^ n... ^ 1000) = T ^ (T ^ n) = 0 ^ n = n

This question ends here.

2. There is only one number in an array once, and all the others appear twice. Find this number.

After understanding the derivation process of the above question, it is very easy to make all the elements in the array exclusive or, at last two elements will be eliminated, in the end, you will get the number that appears only once.

This topic can also be used in the following scenario. The idea is the same: only one number in the array has an odd number, and all others have an even number.


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.