Algorithm - a digital array appear only

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45505313/article/details/102767316

1. Find an array of numbers appear only once

In fact, this topic in two forms:

  1. In addition to an array of integersOne digital, Other figures have emerged twice to find out that only appears once the digital
    如数组 [1,2,2,1,3], 则目标数字为 3

  2. In addition to an array of integersA two numbers, Other figures have emerged twice to find the two appeared only once digital
    如数组 [1,2,2,1,3,5], 则目标数字为 3,5

2. Solution

2.1 is not only a duplicate numbers

Ideas:Using the XOR operator ^, With other numbers 0 XOR result of that number, the exclusive OR to obtain equal numbers 0

  • In addition to the array one number appears only once, the other figures appear twice, so a variable a defined initial value of 0, and a variable array, each figure is an exclusive OR operation, and this variable updates the value of the result of the operation, until the array traversal is completed, the resulting value of the variable is an array has only appeared once that number
 public static int findOnceFrom2(int[] a) {
        int len = a.length, res = 0;
        for (int i = 0; i < len; i++) {
            res = res ^ a[i];
        }
        return res;
    }

2.2 There are two non-repeating numbers

In fact, this problem is based on the expansion of the above problems,First it is seen that when there is only one number one occurrence, the array of all the numbers in sequence exclusive-or operation, what is left is the single number, since pairs are offset.

  • According to this idea, first of all I would first XOR 最后剩下的数字肯定是A、B异或的结果 C,以数组 [1,2,2,1,3,5]为例, 则 A=3(0011),B=5(0101),结果C=6(0110). C-bit binary representation where 1, in fact A 和 B 的二进制表示中数值不同的位. We take the first one where 位数 index, we can see the first two, ie index=2,Thus the original array divided into two groups, the grouping criterion is the number of two is 1. Such a pair of numbers in the affirmative group, the same number as the values ​​of all bits are the same, and certainly not the same number of different groups. After these two groups along the lines of the beginning, followed by XOR, and the remaining two results is that these two figures appear only once
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {  
		 if(array == null || array.length <= 1){
	            num1[0] = num2[0] = 0; // 初始化为 0
	            return;
	        }
	        int len = array.length, index = 0, sum = 0;
	        for(int i = 0; i < len; i++){
	            sum ^= array[i];
	        }
	        for(index = 0; index < 32; index++){
	            if((sum & (1 << index)) != 0) break; // 获取第一个 1 所在位数 index
	        }
	        for(int i = 0; i < len; i++){
	            if((array[i] & (1 << index))!=0){ // 根据 index 将数组分为两部分
	                num2[0] ^= array[i];
	            }else{
	                num1[0] ^= array[i];
	            }
	        }
	        System.out.println(num1[0]+" "+num2[0]);
	        
    }

Guess you like

Origin blog.csdn.net/weixin_45505313/article/details/102767316