Find the number that appears more than half of the number in the array.

Source: Internet
Author: User

Question: If a number in the array appears more than half the length of the array, find the number.

Solution 1: sort the array by fast sorting. Because a number appears more than half of the length of the array, you can directly obtain the number in the middle after sorting!

Time Complexity: O (N * logn), because time is mainly spent on fast sorting!

public static int find1(int[] a) {Arrays.sort(a);int mid = 0 + (a.length - 0) / 2;int result = a[mid];return result;}

Solution 2: Create a hash table to eliminate the sorting time. The key of the hash table is the number in the array, and the value is the number of times that the number corresponds. With this auxiliary hash table, we only need to traverse each number in the array, find its position in the hash table, and increase the number of times it appears. This hash table method is effective when all the numbers in the array are in a narrow range.

// Calculated based on the hash table. The key is a number and the value is the number of times that the key appears. Public static int find2 (INT [] A) {Map <integer, integer> map = new hashmap <> (); map. put (A [0], 1); For (INT I = 1; I <. length; I ++) {If (map. containskey (A [I]) {map. put (A [I], map. get (A [I]) + 1);} else {map. put (A [I], 1) ;}} set <integer> set = map. keyset (); iterator <integer> iterator = set. iterator (); int max =-1; int Index =-1; while (iterator. hasnext () {int key = (INT) iterator. next (); int value = map. get (key); If (value> MAX) {max = value; Index = Key ;}} Return Index ;}

3:

The previous two ideas did not take into account the features of the array in the question: the number of numbers in the array has exceeded half of the length of the array. That is to say, a number appears more times than the sum of all other numbers. Therefore, we can consider saving two values when traversing the array: one is a number in the 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 previously, the number of times is increased by 1. If the next number is different from the number we saved earlier, the number of times is reduced by 1. If the number of times is zero, we need to save the next number and set the number to 1. Because the number we are looking for appears more than the sum of the number of all other numbers, the number we are looking for must be the number corresponding to the last time we set the number to 1.

Public static int find3 (INT [] A) {// initialize the first digit in love, and set the number of times the first digit appears to 1; int COUNT = 1; int number = A [0]; for (INT I = 1; I <. length; I ++) {if (a [I] = Number) {count ++;} else {count --;} If (COUNT = 0) {number = A [I]; Count = 1 ;}} return number ;}




Find the number that appears more than half of the number in the 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.