"One by one" Algorithm

Source: Internet
Author: User
After reading the above solutions, it is always a little too far-fetched. I wrote them for your reference.
Question: 1-1001 is placed in an array containing elements, and only one unique element value is repeated.
Once. Each array element can only be accessed once. design an algorithm and find it.
Can we design an algorithm for implementation?
The time is o (N) and there is no additional storage space (in other words, it means that apart from arrays, there is no additional memory space to store array elements ).

// Initialization
Int N = 10;
Int [] a = new int [N];
Random rand = new Random ();
// Assign a value
For (int I = 1; I <a. Length; I ++)
{
A [I] = I;
}

Int idx1 = 0;
Int idx2 = 0;
// Disrupt the order
For (int I = 0; I <a. Length * 10; I ++)
{
Idx1 = rand. Next (N );
Idx2 = rand. Next (N );
If (idx1 = idx2)
{
I --;
Continue;
}
A [idx1] = a [idx1] ^ a [idx2];
A [idx2] = a [idx1] ^ a [idx2];
A [idx1] = a [idx1] ^ a [idx2];
}
// Create a duplicate Element
Idx1 = rand. Next (N );
While (a [idx1] = 0)
Idx1 = rand. Next (N );

For (int I = 0; I <a. Length; I ++)
{
If (a [I] = 0)
{
A [I] = a [idx1];
Break;
}
}
// Search
Unsafe
{
Fixed (int * p =)
{
Int * p1 = p;
// If a [0] = a [1]
If (* p1 = * (p1 + 1 ))
{
// Find!
Return;
}
// Loop array
While (p1 <p + N)
{
Short * pBigEndian = (short *) (p + * (short *) p1) + 1;
Short * pLittleEndian = (short *) p1;
* PBigEndian = * pLittleEndian;
P1 ++;
PBigEndian = (short *) (p + * (short *) p1) + 1;
PLittleEndian = (short *) p1;
If (* pBigEndian = * pLittleEndian)
{
// Find!
Break;
}
}
}
}

This post has been suspended for one day and there are very few people who can understand me. To save your time, I will explain it as follows,
Assume that the array has 10 elements. The initialization elements are as follows: -A {Dimensions: [0x0000000a]} int []
[0x00000000] 0x00000003 int
[0x00000001] 0x00000008 int
[0x00000002] 0x00000001 int
[0x00000003] 0x00000009 int
[0x00000004] 0x00000007 int
[0x00000005] 0x00000002 int
[0x00000006] 0x00000006 int
[0x00000007] 0x00000005 int
[0x00000008] 0x00000007 int
[0x00000009] 0x00000004 int

It can be seen that the elements of the phase are located at a [4] and a [8], that is, the same elements can be found only when the array loops to a [8.
Loop once: -A {Dimensions: [0x0000000a]} int []
[0x00000000] 0x00000003 int
[0x00000001] 0x00000008 int
[0x00000002] 0x00000001 int
[0x00000003] 0x00030009 int
[0x00000004] 0x00000007 int
[0x00000005] 0x00000002 int
[0x00000006] 0x00000006 int
[0x00000007] 0x00000005 int
[0x00000008] 0x00000007 int
[0x00000009] 0x00000004 int

It can be seen that the value of a [3] has changed to 0x00030009, because I wrote the 16 lower bits of a [0] to the 16 higher bits of a [3. Because the number is 1 ~ 10000, so 16-bit numbers can be expressed. For int, 32-bit numbers cannot be expressed at a high level, so space is available.
The idea is: write the number to the index position in sequence. When you find that the value to be written is equal to the number at the index position (in fact, you can set it to 1 ), is hit.
Next, when the loop runs to a [8: -A {Dimensions: [0x0000000a]} int []
[0x00000000] 0x00000003 int
[0x00000001] 0x00010008 int
[0x00000002] 0x00020001 int
[0x00000003] 0x00030009 int
[0x00000004] 0x00000007 int
[0x00000005] 0x00050002 int
[0x00000006] 0x00060006 int
[0x00000007] 0x00070005 int
[0x00000008] 0x00080007 int
[0x00000009] 0x00090004 int

At this time, the low position of a [8] is 7, so it should be written as a [7] = 7; but at this time, the high position of a [7] is already 7, so it hits, jump out.

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.