Title Description
If there is a number in the array that appears more than half the length of the array, 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.
Method 1: An algorithm based on the partition function
Public classTest { Public Static voidMain (string[] args) {int[] A = {1,1,1,3,4}; System.out.println (Morethanhalfnum (a)); } /*** 1, an array of fast sorting, then the number in the middle of the arrays must be the number of occurrences more than half the length of the array. * @paramArray *@return */ Private Static intMorethanhalfnum (int[] Array) { if(Array = =NULL|| Array.Length <= 0) return0; intLow = 0; intHigh = Array.length-1; intMID = Array.Length >> 1; intindex =partition (array, low, high); while(Index! =mid) {if(Index <mid) {index= partition (Array, index+1, high); } Else{Index= partition (array, low, index-1); } } intresult =Array[mid]; if(!checkmorethanhalfnum (array, result)) { return0; } returnresult; } Private Static BooleanCheckmorethanhalfnum (int[] Array,intresult) { intTimes = 0; for(inti=0; i<array.length; i++) { if(Array[i] = =result) { times++; } } if(Times * 2 <=array.length)return false; return true; } Private Static intPartitionint[] A,intLowintHigh ) { intkey = A[low];//The first number is a key, and the array is divided into two parts while(Low < High) {//left and right not to meet while(Low < High && A[high] >= key)//first from the right side of the array to find the number less than key (must start from the right), ExchangeHigh--; A[low]=A[high]; while(Low < High && A[low] <= key)//from left to right, find the number greater than key, swapLow + +; A[high]=A[low]; } A[low]= key;//key is placed in the right position, the left side is less than key, the right is greater than key returnLow ; }}
Method 2:
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 differs from the number we saved earlier, the number is reduced by 1. If the number is 0, 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.
Public classTest { Public Static voidMain (string[] args) {int[] A = {}; System.out.println (Majorityelement (a)); } Public Static intMajorityelement (int[] a) {if(A = =NULL|| A.length <= 0) return0; intcandidate = a[0];//to record a value that is more than half the length of the array intNtimes = 1;//number of for(inti = 1; i < a.length; i++) { if(Ntimes = = 0) {candidate=A[i]; Ntimes= 1; } Else { if(Candidate = =A[i]) ntimes++; ElseNtimes--; } } if(!Checkmorethanhalfnum (A, candidate))return0; returncandidate; } Private Static BooleanCheckmorethanhalfnum (int[] Array,intresult) { intTimes = 0; for(inti=0; i<array.length; i++) { if(Array[i] = =result) { times++; } } if(Times * 2 <=array.length)return false; return true; }}
A number of more than half occurrences in a offer-array.