The number of occurrences in the array exceeds half the length of the 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 18 {1,0,2,6,1,0,1,1,5,2,1,1,1,3,1,1,5,1}, since the number 1 in the array appears more than half the length of the array, so output 1.

1) The most straightforward way is to sort the array first, then more than half of the elements must be the most intermediate elements of the array.

2) Think about it and think about the high-speed sequencing process, and use partion to find the value of the index corresponding to the index==middle, rather than having to sort it out completely.

3) Another method is more abstract, set a variable to save the current value, set a number of times, the current value is compared with the next value, assuming equal, number of times plus one. Assumptions are not equal. Number minus one, assuming that the number of times is reduced to 0 or not equal. Replace the current value.

This shows only 2), 3) of the source code, no data rationality check.

#include <iostream> #include <vector>using namespace Std;int partion (vector<int>& vec,int Low, int high) {//fast sort base int key = Vec[high];int Fast = Low;int slow = low;while (Fast < High) {if (Vec[fast] <= key) {if (f AST = = slow) slow++;} Else{if (FAST! = slow) {int tmp = Vec[slow];vec[slow] = Vec[fast];vec[fast] = tmp;slow++;}} fast++;} int tmp = Vec[slow];vec[slow] = Vec[high];vec[high] = Tmp;return slow;} int Find_half1 (vector<int> vec) {int length=vec.size (); if (length = = 0) return 0;int start = 0;int end = Length-1;in  T middle = length >> 1;int index = partion (VEC, start, end); while (Index! = middle) {//median value must be a number of more than half occurrences if (Index > middle) {end = Index-1;index = Partion (VEC, start, end);} Else{start = index + 1;index = partion (VEC, start, end);}} cout << "index:" << index << Endl;return vec[index];} int Find_half2 (vector<int> vec) {//Set key and index. Assuming a value equal to key, index++, otherwise index--;//assumes index==0, the value of the key is updated, because the target value appears more than half the number of occurrences, so the index must be greater than 0int key=vec[0];int index = 0;for (int i = 0; i < vec.size (); ++i) {if (vec[i] = = key) {index++;} Else{if (index = = 0) {key = vec[i];index++;} else{index--;}}} Return key;} int main () {int a[18] = {1,0,2,6,1,0,1,1,5,2,1,1,1,3,1,1,5,1};vector<int> VEC (A, A + 18);//Such a container initialization method is very practical for (int i = 0; I < vec.size (); ++i) {cout << vec[i] << "";} cout << Endl << find_half1 (VEC) << Endl;return 0;}




The number of occurrences 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.