Number of occurrences more than half in an array

Source: Internet
Author: User

The title describes a number in the array that appears more than half the length of the array, please 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. Output 0 If it does not exist. Start thinking: The idea is simple, apply for a map. All elements are subscript, and value is the number of occurrences. The code is as follows:
intMorethanhalfnum_solution (vector<int>numbers) {        intLength =numbers.size (); if(Length = =1)returnnumbers[0]; Map<int,int>m;  for(inti =0; i < length; i++) {M[numbers[i]]+=1; }        intLen =m.size ();  for(inti =0; i < Len; i++)        {            if(M[i] > length/2)                returni; }        return 0; }

But map wastes space and time.

Idea two: We are looking for more than half the number of numbers, so it is more than the other variables added together. Set two variables, a record number, a record count, traverse an array, the current number is not equal to the number recorded, the number of times-if the current number = = record number, then the number of times + +. If there is a number that satisfies the condition, that is the number of the variable. It is important to note that the last obtained number is to be verified.

The count is zero only if there is a number in half. Therefore, it is not possible to determine whether there is a qualified value by this condition. If there are no qualifying points, then the variable is recorded as the last value.

Example: 1 2 3 2 4 2 5 2 3 No number is met, then the last num saved is 3

Code:

intMorethanhalfnum_solution (vector<int>numbers) {        intLength =numbers.size (); intNcount =1; intnum = numbers[0];  for(inti =1; i < length; i++)        {            if(Ncount = =0) {num=Numbers[i]; Ncount++; }            Else if(Numbers[i] = =num) {ncount++; }            Else{ncount--; }} ncount=0;  for(inti =0; I < length;i++)        {            if(Numbers[i] = =num) ncount++; }        if(Ncount > length/2)            returnnum; Else            return 0; }

Idea three: After an array is sorted, if the number of qualifying numbers exists, it must be the middle of the array. (e.g. 1,2,2,2,3; or 2,2,2,3,4; or 2,3,4,4,4, etc.)

Although this method is easy to understand, the time complexity of O (NLOGN) is not optimal because it involves the fast sort.

Reference code:

classSolution { Public:    intMorethanhalfnum_solution (vector<int>numbers) {        //because of the sort, time complexity O (NLOGN), is not optimal        if(Numbers.empty ())return 0; Sort (Numbers.begin (), Numbers.end ()); //sort, take the middle of the array        intMiddle = numbers[numbers.size ()/2]; intCount=0;//Number of occurrences         for(intI=0; I<numbers.size (); + +i) {if(numbers[i]==middle) + +count; }                 return(Count>numbers.size ()/2) ? Middle:0; }};

Number of occurrences more than half in 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.