Today, we can seeArticleNo exclusive or is used in the answer.
Bitwise OR operator: ^
Two identical numbers are exclusive by bit or the result is 0.
Any number is equal to zero by bit or the result is the number itself.
Based on the characteristics of bitwise OR, we can give a simpleAlgorithm:
There is a series with a length of 10000, which stores a natural number of 1-, continuous, non-repeating, but the series are not sequential.
Now we randomly select a natural number between 1-and add it to this series. We need to use a simple algorithm to find the number that we add later.
After analysis, this problem cannot be solved by subtracting the sum of numbers in the sequence from the sum of numbers in the sequence to the sum of 1-series. This may cause overflow.
Second, some people think this topic is about ordered arrays. I think it should not be. if the array is ordered, you can quickly and easily find the repeated number by comparing whether the two numbers are different from each other by 1.
This type of problem is very suitable for exclusive or (if it is not a numerical value), because the two features of the bitwise exclusive OR operator mentioned above can be 1 ^ 1 = 0, 1 ^ 1 ^ 2 = 2, 1 ^ 2 ^ 1 ^ 2 ^ 3 = 3...
Therefore, the "exclusive or" sum "of the 10001 numbers in the array is different from 1-or the number that can be repeated is available.
Exclusive or search:
Const Int Max = 10000;
List < Int > List = New List < Int > (MAX + 1 );
For ( Int I = 0; I <Max; I ++)
List. Add (I + 1 );
Random Rand = New Random (datetime. Now. millisecond );
Int Duplicate = Rand. Next (1, max ); // Obtain a random number
Int Dupindex = Rand. Next (0, max );// Obtain the random insert position
List. insert (dupindex, duplicate ); // Insert a random number at a random position, indicating that the sequence is unordered.
Console. writeline ( "The number of duplicates is :" + Duplicate. tostring ());
Int XOR = 0;
For ( Int I = 0; I <= max; I ++)
{
XOR ^ = list [I];
XOR ^ = I;
}
Console. writeline ( "Search Result :" + XOR. tostring ());
Console. readkey ();
It looks more convenient than binary search and binary tree.