"Sword refers to the offer to study" "Face question 40: Only one occurrence of the number in the array"

Source: Internet
Author: User

title: An integer array In addition to the two numbers, the other numbers appear two times, please write the program to find the two appear only once the number. The required time complexity is O (n), and the spatial complexity is O (1). examples Show

For example, the input array {2, 4, 3, 6, 3, 2, 5}, because only 4, 6 of the two numbers appear only once, the other numbers appear two times, so output 4 and 6.

Thinking of solving problems

All two of these topics emphasize that one (or two) numbers appear only once and others appear two times. What's the point? We think of a nature of the XOR operation: any one number XOR itself equals 0. That is, if we vary from start to finish or every number in the array, then the final result is exactly the one that appears only once, because those numbers that have been paired two times are all offset by XOR.
After trying to figure out how to solve this simple problem, let's go back to the original question and see if we can use the same ideas. We try to divide the original array into two sub-arrays, so that each sub-array contains a number that appears only once, while the other numbers appear in pairs two times. If we can split this into two arrays, we can find out two numbers that appear only once in the previous way.
We're still different from beginning to end, or every number in an array, so the result is an XOR result of two occurrences of a single number. Because the other numbers appear two times, they all cancel out in the XOR. Since these two numbers are definitely different, the XOR result is definitely not 0, meaning that at least one of the binary representations of the result number is 1. We find the position of the first 1 bit in the result number, which is recorded as the nth bit. Now we divide the number in the original array into two sub-arrays with the nth bit not 1, and the nth bit of each number in the first Subarray is 1, and the nth bit of each number in the second subarray is 0. Since the standard of our grouping is whether one of the numbers is 1 or 0, the number that appears two times is definitely assigned to the same sub-array. Since any one of the two identical numbers is the same, we cannot allocate two identical numbers to two sub-arrays, so we have divided the original array into two sub-arrays, each of which contains a single occurrence of the number, and the other numbers appear two times. We already know how to find the only one occurrence in the array, so all the problems have been solved.

Code implementation
 Public classTEST40 { Public Static int[]findnumbersappearanceonce(int[] data) {int[] result = {0,0};if(Data = =NULL|| Data.length <2) {returnResult }intXOR =0; for(intI:data) {xor ^= i; }intINDEXOF1 = FindFirstBit1 (XOR); for(intI:data) {if(IsBit1 (i, INDEXOF1)) {result[0] ^= i; }Else{result[1] ^= i; }        }returnResult }Private Static int findFirstBit1(intNUM) {intindex =0; while(Num &1) ==0&& Index < +) {num >>>=1;        index++; }returnIndex }Private StaticBooleanisBit1(intNumintindexbit) {num >>>= indexbit;return(Num &1) ==1; } Public Static void Main(string[] args) {int[] Data1 = {2,4,3,6,3,2,5,5};int[] result1 = Findnumbersappearanceonce (data1); System. out. println (result1[0] +" "+ result1[1]);int[] Data2 = {4,6};int[] result2 = Findnumbersappearanceonce (data2); System. out. println (result2[0] +" "+ result2[1]);int[] Data3 = {4,6,1,1,1,1};int[] Result3 = Findnumbersappearanceonce (DATA3); System. out. println (result3[0] +" "+ result3[1]); }}
Run Results

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Sword refers to the offer to study" "Face question 40: Only one occurrence of the number in the array"

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.