More than half of the numbers in the array (HashMap usage - get)

Question Description There is a number in
an array that appears more than half the length of the array. Find the 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 is more than half the length of the array, 2 is output. Output 0 if not present.

import java.util.Arrays;

    public int MoreThanHalfNum_Solution(int [] array) {      
        Arrays.sort(array);        
        int count = 0;
        int value = array[array.length/2];
        for(int i=0;i<array.length;i++){
            if(array[i] == value)
                count++;
        }
        if(count > array.length/2)
            return value;
        else
            return 0;
    }

2. Based on the idea of ​​quick sorting

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {        
        int low = 0,high = array.length - 1;
        int index = paritition(array,low,high);
        int mid = array.length/2;
        while(index != mid){
            if(index > mid){
                index = paritition(array,low,index - 1);

            }
            else{
                index = paritition(array,index + 1,high);

            }
        }
        int value = array[index];
        int count = 0;
        for(int i=0;i<array.length;i++){
            if(array[i] == value)
                count++;
        }
        if(count > array.length/2)
            return array[array.length/2];
        else
            return 0;
    }
    public int paritition(int [] array,int low,int high){        
        int value = array[low];
        while(low < high){
            while(low < high && array[high] >= value)
                high--;
            array[low] = array[high];
            while(low < high && array[low] < value)
                low++;
            array[high] = array[low];
        }
        array[low] = value;
        return low;
    }
}

Note: dividing partition returns a subscript value, and the number corresponding to this subscript value is correct in this traversal! And there is also a feature that the numbers on the left of the corresponding value of this subscript are all less than it, and the numbers on the right are all greater than it!

My initial idea was to get the corresponding subscript value of the first partition, and directly determine whether the subscript value is equal to mid, if it is equal, ok; if not, continue to partition.
But: It means so, but how to proceed with the subsequent partition? We must be clear that our goal is to find the corresponding value with the subscript mid !
I thought that A[low] might be different every time, so the returned index must be different. => This is wrong! ! !
eg: The first number is 1, and the following numbers are all greater than 1. Then, every time a partition is performed, the obtained index is the same. In this way, it enters an infinite loop!
So how to do it?
Note that the index I returned is the correct position of the corresponding number in the array, and the numbers on the left of the corresponding value of this subscript are all less than it, and the numbers on the right are all greater than it! That is, if the subscript value index > mid, it means that the mid subscript only needs to be searched from 0 to index-1; . .
3. HashMap must be written by everyone, but, look at this way of writing:

import java.util.*;

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {        
        HashMap<Integer,Integer> map = new HashMap();
        for(int i=0;i<array.length;i++){
            if(map.get(array[i]) == null){
                map.put(array[i],1);
            }else{
                map.put(array[i],map.get(array[i]) + 1);                
            }
            if(map.get(array[i]) > array.length/2)
                return array[i];
        }
        return 0;
    }

}

Note: The map.get(x) function, if the map does not contain x, returns null; if it does, it returns the current number of elements! In the end, x is returned, not map. get(x).
Harvest: When inserting an element into the HashMap in the future, first get the element to determine whether it exists!
3. According to the characteristics of the array, there is one number that occurs more times than the sum of all other numbers (that is, there are two types of numbers, one is res and the other is not res). We save two numbers: one is the number of candidates in the array, and the other is count! Initially, the number of times is set to 1. If the current number and the saved number are the same, then the number of times is increased by one; if they are different, the number of times is decreased by one. code show as below:

import java.util.*;

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {        
       int number = array[0];
        int count = 1;
        for(int i=1;i<array.length;i++){
            if(array[i] == number){
                count++;
            }else{
                count--;
                if(count == 0){
                    number = array[i];
                    count = 1;
                }
            }
        }
        count = 0;
        for(int i=0;i<array.length;i++){
            if(array[i] == number)
                count++;
        }
        if(count > array.length/2)            
            return number;
        else 
            return 0;
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325615999&siteId=291194637