To find an issue with an ID of 1 occurrences

Source: Internet
Author: User

Problem description: In a table saved n ID, there are N-1 ID is two times, only one ID appears once, now you want to find out this ID. What if it's a two?Solution One:Let's deal with one first. If the value of the ID range is 1-k, when this k is not big, we can directly open an array, sweep the table again, record the number of times each ID appears, so that the occurrence of the time is out, so that the complexity of the Times is O (n), the space complexity is O (n).Solution Two:However, when K is large and large, this is obviously not the case. Then we propose a second approach to this optimization, which is to hash each ID with a hash, save it in the hash table, and then, when I insert it, I find that this ID has already appeared, the ID is removed from the hash table, and finally there is only one ID, that is, only once the ID. So time complexity is still O (n), in fact, time complexity O (n) there is no space to optimize. The point is, can spatial complexity be optimized to O (1)?Solution Three:Optimized to O (1), that is, only one variable is used, which means that the last value that this variable holds is the ID we're looking for. We can describe this process in this way: ID = f (a[0:n-1]) The key is how to design this f (). Here's a neat way to do it. We know that two numbers are different or have such properties: a ^ a = 0a ^ 0 = A and different or satisfy the commutative law, so if the ID is such a set, we make an XOR or traverse it: a1,a1,a2,a2,a3,a3,..... ak,ak.....an, An we make AK is the ID we are looking for, this ID list is not sequential, that is, the same ID is not necessarily adjacent, so the assumption is this: ak1 ^ AK2 ^ ak3......akn This order we are unknown, but the use of different or meet the characteristics of the Exchange law, Can do this: ak1 ^ ak2 ^ ak3......akn= A1 ^ A1 ^ A2 ^ A2 ^ ak-1 ^ ak+1 ^ ak and a1 ^ a1 = 0, so the former n-1 xor result is 0, the last remaining 0 ^ ak = 0 So , the result of the last XOR is that only one ID appears. And then, what if it's two? What if two A and b,id only appear once? How to find these two IDs in O (n) time and O (1) space? Like the previous method, direct XOR or traversal can only result in two IDs, and cannot get two IDs. We can take advantage of the two ID or the result above the fuss, if the XOR result is 0, the description is two equal ID, here is not to discuss, if the outcome of the XOR is not 0, then the result of at least one is 1, which shows that A and b at least on this one is not equal, so, We can put this one of all the same ID in a class, so that a class is this is a 1 class, we call this is the X category, the other class of this bit is a class of 0, called the Y class, because the other n-2 ID is exactly the same, so if an ID in this bit is 1, Then these two IDs must be divided in the X class at the same time, so the end result is that there is a or B id,y class in the X class has a or B ID, but it is certain that A and B must not be in the same class, so we can separate the two types of IDs or can get a and b

To find an issue with an ID of 1 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.