The teacher left this Microsoft face question and gave the idea
J is used to temporarily store the data in an array, and count is used to store the number of occurrences.
At the beginning, K stores the first number in the array, count is 0, if the array appears in the number of J equals, then J plus 1, or minus 1, if J is 0, the number in the current array is assigned to J
Because the specified number of occurrences is greater than half the length of the array, after all count++ and count--are offset, the last count value is greater than or equal to 1, and the number that is present in J is the number that appears most.
#include <iostream>using namespacestd;intSearchintA[],intsize) { if(NULL = = A | | size<=0) { return-1; } intCount =0; intJ; for(inti =0; i<size; i++) { if(Count = =0) {J=A[i]; Count=1; } Else { if(A[i] = =j) Count++; ElseCount--; } } returnJ;}intMain () {inta[6] = {1,2,2,5,5,5 }; cout<< Search (A,6) <<" "; return 0;}
Find out more than half the number of occurrences in an array (i)