Algorithm learning-array

Source: Internet
Author: User

An array contains 2n + 1 integers, where n numbers appear twice and 1 number appears once. How many times does one appear?

// Method 1: with the aid of an auxiliary array (the length is n + 1, and the element is a struct (including values and
// Count two members), but the time complexity is O (n * n), and the space complexity is O (n + 1)

// Originally, we wanted to define Val as a struct, but since the struct is a value type rather than a reference type,
// The attribute value of the element added to the List combination cannot be modified. assign an element in the list to another Val and modify the values and num in Val,
// The values of Val-related values in the list will not change because they are two different units in the memory.
// In short: Who told me that C is not good at learning? C # is used. Otherwise, C will be used.

   Public     Class  Val {
Public Int Value; // Value
Public Int Num; // Number of occurrences
}

Public Int Finda ( Int [], Int N)
{
List < Val > List = New List < Val > ();
Val val;
Int J = 0 ;
For ( Int I = 0 ; I < N; I ++ )
{
While (J < N)
{
Bool Isexist = False ;
For ( Int K = 0 ; K < List. Count; k ++ )
{
If (List [K]. Value = A [J])
{
Isexist = True ;
List [K]. Num = 2 ;
Break ;
}
}

If ( ! Isexist ){
Val = New Val ();
Val. Value = A [J];
Val. Num = 1 ;
List. Add (VAL );
}
J ++ ;
}
}

Int Result = - 1 ;
Foreach (Val v In List)
{
If (V. Num = 1 ){
Result = V. value;
Break ;
}
}
Return Result;

}

// Method 2: With an array B with a length of n/2 + 1, if the element in a is not in B, it is stored in B,
// If in B, all the elements behind the existing element move forward to one unit, it is equivalent to removing the element that exists in B,
// This one-to-one operation is removed for even times, leaving only elements that appear for an odd number of times.
Public Int Findi ( Int [], Int N)
{
Int [] B = New Int [N / 2 + 1 ];
Int K = 0 ;
For ( Int I = 0 ; I < N; I ++ )
{
Bool Isexist = False ;
For ( Int J = 0 ; J <= K; j ++ )
{
If (A [I] = B [J])
{
Isexist = True ;
For ( Int F = J; f < K - 1 ; F ++ )
{
B [F] = B [F + 1 ];
}
K -- ;
Break ;
}
}

If ( ! Isexist)
{
B [k] = A [I];
K ++ ;
}
}

Return B [ 0 ];

}


// Method 3: sort. Of course the equal numbers are all together. The separate number is the one that only appears once. Haha...
Public Int Finds ( Int [], Int N)
{
For ( Int I = 0 ; I < N - 1 ; I ++ )
{
For ( Int J = I + 1 ; J < N; j ++ )
{
If (A [I] > A [J])
{
Int Temp = A [J];
A [J] = A [I];
A [I] = Temp;
}
}
}

Int Result = - 1 ;
For ( Int I = 0 ; I < N; I = I + 2 )
{
If (A [I] ! = A [I + 1 ])
{
Result = A [I];
Break ;
}
}
Return Result;
}



// Method 4: exclusive or operation (this handsome guy in blog Park)
// The XOR operation 0 is different from any number or equal to any number. The two equal numbers are different or equal to 0,
// That is, the binary bit corresponding to two numbers is used for the XOR operation. 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 1 ^ 1 = 0
// Even times are finished, and the remaining times are odd.
Public Int Findspecial ( Int [], Int N)
{
Int Res = 0 ;
For ( Int I = 0 ; I < N; I ++ )
{
Res = Res ^ A [I];
}
Return Res;
}

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.