The number that appears only once in the array of topic 1
subject
Except for two numbers in an integer array, other numbers appear twice. Please write a program to find out the two numbers that only appear once
Title Solution
- XOR.
- Consider first: only one number in the array appears only once, and other numbers appear twice. How to find this number? All exclusive or, the result is the number.
- Then, the original problem can divide the original array into two sets. If both sets meet the above problem, then two numbers that appear once can be found. How to group them? Group: XOR all numbers. At least one bit must be 1, because there are two different numbers. First find out this bit, and then divide it into two groups according to 0 / 1 of this bit. The two groups naturally meet the above requirements, and the results can be obtained.
- To sum up, step 1: group, step 2: get all exclusive or numbers.
Relevant
- XOR operations can be initialized to 0 because 0 is itself XOR with any number.
- Find the last 1: num & (~ num + 1)
Code
public class Main { public static void main(String[] args) { int[] arr= {2,4,3,6,3,2,5,5}; int[] num1=new int[1]; int[] num2=new int[1]; FindNumsAppearOnce(arr,num1,num2); System.out.println(num1[0]); System.out.println(num2[0]); } //Num1 and num2 are arrays of length 1. out parameter //Set num1[0],num2[0] to return results public static void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) { if(array==null||array.length<2) { return; } //Exclusive or of all numbers int xorResult=0; for(int i=0;i<array.length;++i) { xorResult^=array[i]; } //Find the bit with (last) 1 int bitPos=findFirstOnePos(xorResult); num1[0]=0; num2[0]=0; for(int i=0;i<array.length;++i) { if((array[i]&bitPos)==0) {//bitPos with bit 0 are divided into a group num1[0]^=array[i];//If all the numbers in the group are exclusive or, the result is the number of the first occurrence. } else {//Those with bitPos of 1 are divided into one group num2[0]^=array[i];//If all numbers in the group are exclusive or, the result is the second occurrence of the number. } } } //Get the location of the last 1 of the binary private static int findFirstOnePos(int num) { return num&(~num+1); } }
The only number in the array of Topic 2 that only appears once
subject
In an array, a number appears only once, and other numbers appear three times. Find the number that appears only once
Title Solution
Use a 32-digit group to record the sum of the corresponding bits of each number. The result of each% 3 is the number.
Time complexity O(n), space complexity O(1).Other solutions: sorting, searching, time complexity O(nlogn); or hash table, space complexity O(n).
Relevant
It involves knowing which bits are 1 and getting the final number by displacement sum and operation.
Code
public class Main { public static void main(String[] args) { int[] arr= {1,2,3,3,2,1,4,1,2,3}; System.out.println(occurOnce(arr)); } public static int occurOnce(int[] nums) { int[] bitSum=new int[32]; for(int i=0;i<nums.length;++i) { int bitMask=1; for(int j=0;j<31;++j) { if((nums[i]&bitMask)!=0) { ++bitSum[j]; } bitMask<<=1; } } int ans=0; for(int i=31;i>=0;--i) {//If there is too much high shift, traverse from high to low. ans<<=1; ans+=bitSum[i]%3; } return ans; } }