10 thousand unordered numbers search for two duplicates, same or Lookup

Source: Internet
Author: User

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.

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.