The sword refers to the offer surface question (Java Edition): The number of the array appears once

Source: Internet
Author: User

Title: In an integer array, except for two digits, the other numbers appear two times.

* Please use some procedures to find out the two only occurrences of the number. Requires a time complexity of O (n) and a space complexity of O (1)

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

This is a difficult topic, very few people in the interview without a hint at the moment to think of the best solution. Usually when the candidate thought for a few minutes after that there is no idea, the interviewer will give some tips.

We think of a nature of XOR: Any number xor itself equals 0, that is, if we are different from beginning to end or every number in the array, then the final result is exactly the number that appears once, because the number of pairs that appear two times is different or medium-low.

We try to divide the array into two sub-arrays, so that each sub-array contains only one occurrence of the number, 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 still start from beginning to end, or every number in an array, then 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, denoted as nth bit. Now we divide the number in the original array into two sub-arrays with the nth bit is not 1, and the nth bit of each number in the first Subarray is 1. 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 containing a number that appears only once, and the other numbers appear two times. We already know how to find the only one occurrence in the array, so all the problems are solved in this position.

For example, the number added to our input is {2,4,3,6,3,2,5,5}. When we make an XOR operation of each number in the array, the resulting result is represented by a binary representation of 0010. The second-to-last digit in the result of XOR or obtained is 1, so we are 1 divided into two arrays according to the second-to-last digit. All the digits in the first Subarray {2,3,6,3,2} are 1, and the second sub-array {4,5,5} has the 2nd-lowest of all digits 0, so as long as each of the two sub-arrays is different or, the number that appears only once in the first sub-array is 6. The second sub-array that appears only once is the number 4.

The Java implementation code is as follows:

/** * In addition to two digits in an integer array, the other numbers appear two times. * Please use some procedures to find out the two only occurrences of the number. Requires a time complexity of O (n), Space complexity O (1) */package swordforoffer;/** * @author Jinshuangqi * * August 10, 2015 */public class E40numbersappearo nce {public void findnumsappearonce (int[] arr) {if (arr = = null) return;int number = 0;for (int i:arr) Number^=i;int index = f INDFIRSTBITIS1 (number), int number1= 0,number2 = 0;for (int i:arr) {if (IsBit1 (I,index)) number1^=i;elsenumber2^=i;} System.out.println (NUMBER1); System.out.println (number2);} private int findFirstBitIs1 (int number) {int indexbit = 0;while ((number & 1) = = 0) {Number = number >> 1;++indexbit ;} return indexbit;} Private Boolean isBit1 (int number,int index) {number = number >>index;return (number & 1) = = 0;} public static void Main (string[] args) {int[] arr={6,2,4,3,3,2,5,5}; E40numbersappearonce test = new E40numbersappearonce (); test.findnumsappearonce (arr);}}


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

The sword refers to the offer surface question (Java Edition): The number of the array appears once

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.