Number of occurrences more than half in an array

Source: Internet
Author: User

Topic

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.

Solving

Method One: Quick-line thinking
An element in an array that appears more than half the length of the array, the number must be the median after the array is sorted
But it's not the median number when it doesn't exist.
Solve with the idea of fast-line
Just need to find the number in the middle of the position.
The next step is to find the middle position subscript.
Use the fast row to divide each array, when dividing the position in the middle position, stop dividing
Here's the number of times the middle position is detected.

 Public classSolution { Public intMorethanhalfnum_solution (int[]Array) {if(Array= = NULL | |Array. length = =0)return 0;intleft =0;intright =Array. Length-1;intMiddle = (right+1)/2;intindex = partition (Array, left,right); while(middle! = Index) {if(Index >middle) {right = index-1; index = partition (Array, left,right); }Else{left = index+1; index = partition (Array, left,right); }        }intNumber =Array[Middle];if(Check (Array, Number,middle)) {returnNumber }Else             return 0; } PublicBoolean Check (int[]Array,intValintMiddle) {intCount =0;inti = middle;intj = i+1; while(i>=0&&Array[I] = = val) {i--;        count++; } while(j<Array. length &&Array[j] = = val) {j + +;        count++; }returncount*2>=Array. length; }//Division location     Public intPartitionint[] A,intLow,intHigh) {intx = A[high];inti = low-1; for(intj = low;j<= High-1; j + +) {if(A[j] <= x) {i = i +1;            Swap (A,I,J); }} i = i +1; Swap (A,i,high);returnI } Public voidSwapint[]Array,intIintj) {intTMP =Array[i];Array[I] =Array[j];Array[j] = tmp; }}

Method Two: Find the largest number of occurrences

Traversing an array holds two values: one is a number in a number, and the other is the number of times. When traversing to the next number, if the next number is the same as the number saved before, the number of times is one, and if the next number is different from the number previously saved, the number is reduced by 1. If the number is 0, save the next number and set the number of times to 1.
Since the number we find appears to be more than the sum of the other occurrences, the number to be found is definitely the last number to be set to 1.

 Public classSolution { Public int morethanhalfnum_solution(int[] A) {if(A = =NULL|| A.length = =0)return 0;intnum = a[0];intTimes =1; for(inti =1;i< a.length; i++) {if(A[i] = = num) {//equal number + 1times++; }Else{//Not Equal                if(times>0){//number-1times--; }Else{num = a[i];//Update comparison and number of timesTimes =1; }}} times =0; for(inti =0; i<a.length;i++) {if(A[i] = = num) times++; }if(Times> a.length/2)returnNumreturn 0; }}

Method Three: HashMap
Number of statistics

Import java.util.*; Public classSolution { Public int morethanhalfnum_solution(int[] A) {if(A = =NULL|| A.length = =0)return 0;if(A.length = =1)returna[0]; hashmap<integer,integer> map =NewHashmap<integer,integer> (); for(inti =0; i<a.length;i++) {if(Map.containskey (A[i])) {map.put (A[i],map).Get(A[i]) +1);if(Map.Get(A[i]) > a.length/2)returnA[i]; }Else{Map.put (A[i],1); }        }return 0; }}

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.