Find the unique repeating element in the array

Source: Internet
Author: User

Topic:

1-n is placed in an array of N elements (n=1001), and only one element value is duplicated, and the others appear only once. Each array element can only be accessed once, design an algorithm, find it out, can you design an algorithm implementation without secondary storage space?

(1) Method one: (Alert for overflow when n is relatively large)

Add 1001 elements minus three-,...... 1000 The summation of the sequence, and the resulting difference is the repeating element.

int Find (int* a)

{

int i;

for (i = 0;i<=1000;i++)

A[1000] + = A[i];

A[1000]-= (i* (i-1))/2//i value is 1001

return a[1000];

}

(2) Method two:

The array value operation can be seen as a special function f:d→r, which defines the field as the subscript value 0~1000, with a range of 1 to 1000. If for any number I, we call F (i) its successor, I is called the precursor of F (i). 0 There are no precursors to follow, other numbers have both successors and precursors, and the repetition of that number has two precursors, and we will take advantage of these characteristics.

Rule : Draw an arrow starting from 0 to point to its successor, which continues to point to subsequent successors from its successor, so that there is bound to be a node pointing to the number that has occurred before, that is, the number of repetitions.

Using the special relationship between the subscript and the contents stored in the cell, the Traversal access unit is used, once the accessed unit is given a tag, the mark is the key to discovering the duplicate number. The code is as follows:

void findrepeat (int array[], int length)

{

int index = 0;

while (true)

{

if (array[index]<0)

Break

Array[index] *=-1; Visited, turned to the opposite number

index=array[index]* (-1);

}

cout<< "The repeat number is" <<-array[index] <<endl;

}

(3) method three

Also consider the relationship between the subscript and the content, but without marking, and with two different speed of the process to access. Slow each time before further, fast each step forward two steps. In a ring structure, they always meet.

void findrepeat (int array[], int length)

{

int slow=fast= 0;

while (true) {

slow = Array[slow];

Fast = Array[array[fast]];

if (slow = = fast)

Break

}

Fast = 0;

while (true) {

Slow= Array[slow];

Fast =array[fast];

if (slow = = fast)

Break

}

cout<< "The repeat number is" << Array[slowendl;

}

(4) Method four: XOR or operation

The number of N in the array a[n] is different or the result is different from the result of 1 to N-1, and the resulting value is the request.

    • The number of repetitions is a, the remaining N-2 number is different or the result is B.
    • n number is different or result is a^a^b
    • 1 to N-1 xor or result is a^b
    • Due to the different or satisfying commutative law and binding law, and x^x = 0 0^x = X;
    • Then there are
    • (a^b) ^ (a^a^b) =a^b^b=a

void findrepeat (int array[], int length)

{

int result = 0;

for (int i=1;i<=1000;i++)

Result ^= i;

for (int i=0;i<=1000;i++)

Result ^= Array[i];

cout << result << Endl;

}

Find the unique repeating element in the array

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.