Title: There is a number in the array that appears more than half the length of the array, please find this numerical example description:
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.
Problem Solving Ideas:
Solution One: O (n) algorithm based on partition function
There is a number in the array that appears more than half the length of the array. If this array is sorted, then the number in the middle of the array after sorting must be the number that appears more than half the length of the array. That is to say, this number is the median of statistics, that is, the number of N/2 in an array of length n.
This algorithm is inspired by the fast sorting algorithm. In the random fast sorting algorithm, we first randomly select a number in the array and then adjust the order of the numbers in the array so that the number smaller than the selected digit is on its left, and the number larger than the selected number is on its right. If the subscript of the selected number is exactly N/2, then this number is the median of the array. If its subscript is greater than N/2, then the median should be on the left side of it, and we can then find it in the array on the left part of it. If its subscript is less than N/2, then the median should be on its right, and we can then look in the array on the right side of it. This is a typical recursive process.
Solution Two: Find an O (n) algorithm based on the characteristics of array groups
There is a number in the array that occurs more than half the length of the array, which means that it appears more often 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, if the next number is the same as the number we saved earlier, the number plus L: If the next number is different from the number we saved earlier, the number is reduced by 1. If the number of times is moldy, we need to save the next number and set the number to 1. Since the number we are looking for appears to be more than the sum of the number of other numbers appearing, the number to be searched is definitely the last number to be set at 1 o'clock.
The second way of realization
Code implementation:
Public class Test29 { /** * Title: There is a number in the array that appears more than half the length of the array, please find this number * * @param Numbers input array * @return the number found */ Public Static int Morethanhalfnum(int[] numbers) {//Input checksum if(Numbers = =NULL|| Numbers.length <1) {Throw NewIllegalArgumentException ("Array length must large than 0"); }//Used to record the number of occurrences greater than half the array intresult = numbers[0];Number of different numbers for the current record intCount =1;//start looking backwards from the second number for(inti =1; i < numbers.length; i++) {//If the count is 0 if(Count = =0) {//re-record a number, assuming it is more than half the number of occurrences of the arrayresult = Numbers[i];//Record statistical valuesCount =1; }//If the recorded value is equal to the statistic value, the recording value increases Else if(Result = = Numbers[i]) {count++; }//If not the same, reduce, offset each other Else{count--; } }//The last result may be a value that occurs more than half the length of the array //Statistics number of occurrences of resultCount =0; for(intNumber:numbers) {if(Result = = number) {count++; } }//If the number of occurrences is greater than half of the array, the corresponding value is returned if(Count > Numbers.length/2) {returnResult }//Otherwise input exception Else{Throw NewIllegalArgumentException ("Invalid input"); } } Public Static void Main(string[] args) {//number of occurrences exceeding half the length of the array intNumbers[] = {1,2,3,2,2,2,5,4,2}; System.out.println (Morethanhalfnum (numbers));number of occurrences exceeding half the length of the array appears in the first half of the array intNumbers2[] = {2,2,2,2,2,1,3,4,5}; System.out.println (Morethanhalfnum (numbers2));number of occurrences exceeding half the length of the array appears in the second half of the array intNumbers3[] = {1,3,4,5,2,2,2,2,2}; System.out.println (Morethanhalfnum (NUMBERS3));//Only one number intNumbers4[] = {1}; System.out.println (Morethanhalfnum (NUMBERS4));//input NULL pointerMorethanhalfnum (NULL);//There are no numbers that occur more than half the length of the array intNumbers5[] = {1,2,3,2,4,2,5,2,3}; Morethanhalfnum (NUMBERS5); }}
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Sword refers to offer study" "Face question 29: The number of occurrences in an array is more than half"