First, let's start with a simple question: In an array, all the numbers except one appear twice. How can we find the different number,
Simply put, all numbers are different or the final result is the number to be searched, because the same number is different or the result is 0.
Now let's think about this question:
The book "The beauty of programming" provides a way to convert all numbers into an exclusive or, get a number, and then use a non-zero value of the number as a filter bit, divide the array into two parts. The number displayed at this time is divided into different parts. Now the problem has been converted to one:
AlgorithmAs follows:
Public int [] findtwodiff (INT [] A) {int [] result = new int [2]; int temp = 0; int flag = 1; for (int t:) {temp ^ = T;} while (temp & flag) = 0) {flag <= 1 ;}for (int t:) {If (T & flag) = 1) {result [0] ^ = T;} else {result [1] ^ = T ;}} return result ;}