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.


My thoughts.

If the problem is solved directly, there is no difficulty. You can use a map to save the number of occurrences of each number, and then look for the number of occurrences that are greater than half the length of the array. After the submission, there are a lot of different solutions to the comments, some sorted first, and then the median number of occurrences is more than half the length of the array (because if there is such a number, it must be the median of the sort). There are some mathematical laws, such as: If a number appears more than half the length of the array, then it will appear more often than the sum of the number of occurrences of all other numbers.


Direct solution
import Java.util.HashMap;import Java.util.Map; Public classSolution { Public int morethanhalfnum_solution(int[] array) {Map<integer, integer> Map =NewHashmap<> (); for(intI:array) {if(Map.ContainsKey(i)) {map.put(i, map.)Get(i) +1); }Else{map.putI1); }        } for(Map.Entry<integer, integer> E:map.EntrySet()) {if(E.GetValue() > Array.length/2)returnE.GetKey(); }return 0; }}


Through mathematical laws

Save two values when iterating through an array: One is a number in an array, and the other is the number of times. When traversing the next number, if it is the same as the previously saved number, the number of times is 1, otherwise the number is reduced by 1, and if the number is 0, the next number is saved and the number is set to 1. After the traversal is completed, the saved numbers are the desired. Then judge if it meets the criteria.


 Public classSolution { Public int morethanhalfnum_solution(int[] array) {intresult =0;intCount =0; for(intI:array) {if(Count = =0) {result = I;            count++; }Else if(Result = = i)            {count++; }Else{count--; }        }if(Count = =0)return 0; for(intI:array) {if(i = = result) count++; }if(Count > Array.)length/2)returnResultreturn 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.