More than half the number of occurrences in an array of C + + algorithms

Source: Internet
Author: User

Title Description:

If there is a number in the array that appears more than half the length of the array, find this number. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears in the array 5 times, which exceeds half the length of the array, the output is 2.

See this topic, I think of the method:

Another application for an array B, to hold the number of occurrences of the array a element, and then traverse the B array, the number of occurrences is the most; but this if the array of elements in a is 1000, then the B array will apply at least 1000, the space waste is too serious.

Method Two:

Similar to the elimination principle, since a number is greater than half of the length, then we iterate over the array, if the two numbers are not equal to eliminate, the last remaining number is what we want. Of course, if there is no such number, it is not possible. So I'm going to go through it again to verify that the number of occurrences is greater than half of the array.

We keep two values when we iterate over an array, one is a number in the array, and the other is the number of times. When we traverse to the next number, if the next number is the same as the number we saved earlier, the number is 1, and if the next number is different from the number we saved earlier, the number is reduced by 1. If the number is 0, we want to save the next number and set the number of times to 1

For example 1 2 3 2 2 2 5 4 2

Traverse 1 Value result = 1, count = 1; traverse 2, 1! = 2, Count = 1-1=0; Traverse 3 result = 3, conut = 1; ,。。。。。。。 In turn

MoreThanHalfNum.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace Std;bool g_inputinvalid = True;bool Checkinvalidarray ( int Array[],int nlength) {g_inputinvalid = false;if (array = = NULL | | nlength <= 0) {g_inputinvalid = true;} return g_inputinvalid;}  BOOL Checkmorethanhalf (int array[], int nlength, int result) {int count = 0;for (int i = 0; i < nlength; ++i) {if (Result = = Array[i]) {++count;}} BOOL Ismorethanhalf = true;if (count * 2 <= nlength) {g_inputinvalid = True;ismorethanhalf = false;} return ismorethanhalf;} void FindKey (int array[], int nlength) {//Determine if the array is valid if (Checkinvalidarray (array,nlength)) {return;} int result = Array[0];int count = 1;for (int i = 1; i < nlength; ++i) {if (count = = 0) {result = Array[i];count = 1;} Else{if (Result = = Array[i]) {++count;} Else{--count;}}} Determine if the array conforms to the standard, excluding the array of inputs that does not have more than half of the array if (checkmorethanhalf (array,nlength,result) = = 0) {result = 0;} if (result = = 0) {cout<< "Your array does not appear in more than half the number of numbers" &LT;&LT;ENDL; Else{cout<< " The number of occurrences of more than half of your array is: "<<result<<endl;}} int _tmain (int argc, _tchar* argv[]) {int array[9] = {1,2,3,2,8,2,2,9,2}; FindKey (array,9); GetChar (); return 0;}


Note that if there are no more than half the length of the array, consider the case!

Method Three:

Sort first, and then the number in the middle is definitely the number we're looking for.

But the general sorting algorithm has to be O (N*LGN); I thought of that method. Time complexity is O (n), but the space sacrifice is too large;

Anyway, two methods are not very good.

The best choice is the second method!

More than half the number of occurrences in an array of C + + algorithms

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.