Sword refers to the number of more than half occurrences in an array of offer_. Sword refers to an offer

Source: Internet
Author: User

This problem is not difficult, the reason for the record is: I used the hashmap to record the number of each occurrence and the number of occurrences. In some written interview, there will always be similar problems, but can tell you the array is very large, there are thousands of data (NetEase Cloud music playlist, Baidu search rankings, etc.), at this time with HashMap to solve the perfect, here a bit overqualified ...

Topic description

There is a number in the array that appears more than half the length of the array, please find this number. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Because the number 2 appears in the array 5 times, it exceeds half the length of the array, so output is 2. Output 0 If it does not exist.

Ideas for solving problems:

1. First thought, traversing the array, using HASHMAP to record each number and number of occurrences. Time Complexity of O (n)

2. Array ordering, then the median value must be the value to look for. Sort the smallest time complexity (quick sort) O (NLOGN), plus traversal.

3. If a number appears more than half the length of the array, the number appears more often than the other number. Consider deleting two different numbers at a time, and in the remaining number, the number of occurrences is still more than the sum of the total, repeating the process repeatedly, excluding the others, and eventually the remaining numbers are to be found (if present). The time complexity of this method is O (N), and the space complexity is O (1).

The best use of the hash table, and the complexity of time is low enough to achieve less difficult.
The code is as follows:

import java.util.*; public class Solution {public int morethanhalfnum_solution (int [] array) {if (array==null| |
        array.length==0) {return 0;
        } hashmap<integer,integer> map = new hashmap<integer,integer> (); for (int i=0;i<array.length;i++) {if (Map.get (array[i))!=null) {Map.put (array[i],map.get
            Y[i]) +1);
            }else{Map.put (array[i],1);
            } if (Map.get (Array[i]) >array.length/2) {return array[i];  
    } return 0; }
}

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.