[Array] Numbers that appear more than half the number of times in the array

Title description

The number of occurrences of a number in the array exceeds half of the length of the array. Please find out this number. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears 5 times in the array, which is more than half the length of the array, 2 is output. If it does not exist, output 0.


Sorting method

Sort the array. If the number of occurrences of a certain number exceeds half of the length of the array, the number must appear at the median position, and then traverse the array once to determine whether the number of occurrences is greater than half of the length of the array.

import java.util.Arrays;

public class Solution {
    
    
    public int MoreThanHalfNum_Solution(int[] array) {
    
    
        Arrays.sort(array);
        int res = array[array.length / 2];
        int count = 0;
        for (int i : array) {
    
    
            if (i == res)
                count++;
            if (count > array.length / 2)
                return res;
        }
        return 0;
    }
}

Hashing

Use a hash table to store the number of occurrences of the value in the array

import java.util.ArrayList;
import java.util.HashMap;

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

Optimal solution

If the number of occurrences in the array exceeds half of the length of the array, then the number of occurrences of this number will exceed the sum of the occurrences of all other numbers. You can use two variables countand resrecord the number of occurrences of the element and the value of the element respectively. Note that here The number of times is not the true number of occurrences of the element. Initialization count=1, res=array[0], then through the array, and if the current element values resare the same, count++otherwise count--, if count=0when the resset value of the current element, and count=1. If there is an element that exceeds half of the array length, then the last reselement must be this element, and it count>0will not be reduced to 0 because it appears more often than the other elements of the array, so of course it count>0does not mean resthat The number of occurrences exceeds half of the length of the array, because there may not be an element in the array whose number of occurrences exceeds half of the length of the array, and this can still be satisfied count>0. Therefore, it is necessary to traverse the array again to determine whether resthe number of occurrences is greater than half of the length of the array

import java.util.ArrayList;

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

Guess you like

Origin blog.csdn.net/weixin_43486780/article/details/113729506