Number of occurrences more than half in an array

Source: Internet
Author: User

Title: There is 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.

Analysis: There is a number in the array that occurs more than half the length of the array, which means that it appears more times than all other numbers appear. So we can consider saving two values when iterating over an array: One is a number in an array, and the other is the number of times. When we traverse to the next number, the number is 1 if the next number is the same as the number we saved earlier, minus 1 if the next number differs from the number we saved earlier. If the number is 0, we need to save the next number and set the number of times to 1.

It is important to note that if there are no more occurrences in the array than the average number of array lengths, then the last count of the last count is 1 and the number returned is obviously incorrect. It is therefore possible to consider the following two cases if there are the largest number in the array:

1, the count of the penultimate number is greater than or equal to 2;

2, the count of the second-to-last number equals 1 (in the preceding number, the number of occurrences of the super-array half and the other number is half), and the final number is equal to the second-lowest number;

With these conditions in mind, the following code can be written:

1 intMorethanhalfnum_solution (vector<int>numbers) {2         intMaxnum =0;3         if(Numbers.empty ())4             returnMaxnum;5Maxnum = numbers[0];6         intNumcount =1;7          for(intI=1; I<numbers.size ()-1; i++){8             if(Numbers[i] = =maxnum) {9numcount++;Ten}Else{ Onenumcount--; A                 if(Numcount = =0){ -Numcount =1; -Maxnum =Numbers[i]; the                 } -             } -         } -         if(Numcount = =1&& numbers.back () = = Maxnum) | | Numcount>1) +             returnMaxnum; -         Else return 0; +}

Results of on-line test OJ system: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163?tpId=13&tqId=11181&rp=2 &ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

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