The number of times in the array exceeds half the length of the array

Source: Internet
Author: User

problem: There is a number in the array that appears more than half the length of the array, please find this number.

Analysis: The first reaction to seeing the problem is to sort the numbers in the array, and then count the number of occurrences. The time complexity for sorting is O (Nlogn).

From another angle, there is a number in the array that appears more than half the length of the array, meaning that it appears more often than the number of other occurrences. You can therefore save two values while iterating through the array: (1) saving the values in the array, and (2) saving the number of occurrences.

Step:(1) When the next number of the Traverse is not the same as the previously saved number, the number will be reduced by 1;

   (2) when the next digit of the Traverse is the same as the previously saved number, the number of times is added to 1;

(3) when the number of times is 0 o'clock, the next traversed number is saved, and the number is set to 1.

Since the number we're looking for is more than the number of other numbers, the number we're looking for is definitely the last number to be set to 1.

 PackageCom.wyl;/*** Look for arrays with more occurrences than array lengths *@authorWyl **/ Public classMorethanhalfnum {/*** When traversing an array, save two values: One is the value in the array, one is the number of times to iterate through the array, and if the next number traversed is different from the previously saved value, the number is reduced by 1; * If the same value as previously saved, the number of times plus 1; * If the number is 0 o'clock, save The next number, and the number is set to 1 **/     Public intMorethanhalfnum (int[] Array,intlength) {        if(Array = =NULL|| Length <= 0) {//the input array is illegal            return0; }        intNumber = 0;//save a number in an array        intCount = 0;//number of occurrences of saved numbers         for(inti=0;i<length;i++){            if(count = = 0) { number=Array[i]; Count= 1; }Else if(Array[i] = =Number ) {Count++; }Else{Count-- ; }        }        if(!checknummorethanhalf (array, length, number)) {            return0; }        returnNumber ; }    /*** Check to see if number number is greater than normal in array@paramArray *@paramlength *@paramNumber *@return     */    Private BooleanChecknummorethanhalf (int[] Array,intLengthintNumber ) {        //TODO auto-generated Method Stub        BooleanIsmorethanhalf =true; intCount = 0;  for(inti=0;i<length;i++){            if(Array[i] = =Number ) {Count++; }        }        if(Count*2 <=length) {Ismorethanhalf=false; }        returnismorethanhalf; }         Public Static voidMain (string[] args) {morethanhalfnum morethanhalfnum=NewMorethanhalfnum (); int[] A = {1,0,3,0,0,0,5,4,0,3}; intnum =Morethanhalfnum.morethanhalfnum (A, a.length);    SYSTEM.OUT.PRINTLN (num); }}

The number of times in the array exceeds half the length of 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.