Algorithm -- find out the number of times the array appears more than half

Source: Internet
Author: User

Whenever I see a classic algorithm question, I miss my high school. I feel that many algorithm questions are the question of high school. Who is my brother? I have read-only specialists, and I have almost never learned a high number of questions.
If you have time, you need to look at high numbers. I think that's a good math ......


# Include <iostream>
Using namespace std;
Class FindTheOne
{
Public:
Method 1
The first way to think of it is to see a two-dimensional array, where the data in the one-dimensional array and the number of times the two-dimensional storage occurs. The number that appears the most is the number that you want to find.
Because a number appears more than half the length of the array, the length of the two-dimensional array is only half of the length of the array. The code implementation is as follows,
Of course, this method is very bad. The time complexity and space complexity are both relatively large. I wrote about it if I want to practice it.

  

Method 1
Void Search (int A [], int len, int & theOne)

{

If (NULL = A | len <= 0)

{

Return;

}

Int (* B) [2] = new int [len/2] [2];

B [0] [0] = A [0];

B [0] [1] = 1;

Int t = 0;

Bool notExist = true;

For (int I = 1; I <len; ++ I)

{

For (int j = 0; j <t; ++ j)

{

If (A [I] = B [j] [0])

{

B [j] [1] ++;

NotExist = false;

Break;

}

}

If (notExist)

{

B [t ++] [0] = A [I];

}

}

Int max = 1;

Int k = 0;

For (int I = 0; I <len/2; ++ I)

{

If (B [I] [1]> max)

{

Max = B [I] [1];

K = I;

}

}

TheOne = B [k] [0];

}

Method 2
Sort the array. The number in the middle is the number you are looking.
If the maximum number is the smallest, then 1 to (n + 1)/2 are all
If the maximum number is the largest, then (n-1)/2 to n are all
If it is neither the smallest nor the largest, when the number gradually changes from the smallest to the largest, you will find that the value of the number in the middle remains unchanged.
To sum up, the number in the middle is the number you are looking.
Time complexity is the time used for sorting. I really don't want to write about sorting (refer to my other blog). We all know that sorting is quite time-consuming. Of course this method is still not very good.

Method 3
This method borrowed others' ideas.
Here I will perform a simple analysis.
The time complexity of this algorithm is O (n), and two auxiliary variables are used.
K is used to temporarily store the data in the array, and j is used to store the number of occurrences of a certain number.
At the beginning, k stores the first number in the array, and j is 0. If the number in the array is equal to k, j adds 1; otherwise, 1 is subtracted. If j is 0, then, the number in the current array is assigned to k.
Because the number of occurrences of the specified number is more than half the length of the array, After all j ++ and j -- are offset, the final j value is greater than or equal to 1, the number stored in k is the one with the largest number.

The following algorithm is only applicable to an array with more than half the length of an array.

Method 3
Int Search (int A [], int len)

{

If (NULL = A | len <= 0)

{

Return-1;

}

Int k, j = 0;

For (int I = 0; I <len; ++ I)

{

If (j = 0)

{

K = A [I];

}

If (k = A [I])

{

++ J;

// Some people say that ++ j has inherent advantages over j ++. I wonder if you have heard of it. If you have heard of it, have you ever thought about More Effective C ++ (C ++ programmers must read books)

}

Else

{

-- J;

}

}

Return k;

}

}

;

Related Article

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.