Numbers that occur more than half of the time in an array-Java

Numbers that occur more than half of the time 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 {1,2,3,2,2,2,5,4,2} with a length of 9. 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.

enter

[1,2,3,2,2,2,5,4,2]

return value

2

The intuition of seeing the code is to do it with map, traversing the stored value and the number of occurrences. It can be done without much difficulty, but it takes up a lot of memory. Think of a way to optimize.

Method one (easy, easy. No, no, you can't do it)

import java.util.HashMap;
public class Solution {
    
    
    public int MoreThanHalfNum_Solution(int [] array) {
    
    
        HashMap h = new HashMap<Integer,Integer>();
        if(array.length == 1)
            return array[0];
        for(int i = 0;i<array.length;i++){
    
    
            int cur = array[i]; // 当前数值
            if(h.get(cur) == null){
    
     //如果为空表示第一次出现
                h.put(cur,1);
            }else{
    
     //不为空在基础上加一
                int num = (Integer)h.get(cur) + 1;
                h.put(cur,num);
                if(num == array.length/2 + 1){
    
     //如果有出现次数大于一半退出循环
                    return cur;
                }
            }
        }
        return 0;
    }
}

Well, I didn't want to understand it myself. I went to see the solution, and the idea of ​​solving the problem was really amazing. . Is this the algorithm? Love love

Assuming that different numbers represent different camps, each soldier cannot tolerate the enemy and would rather die with the enemy. It is conceivable that if the number of soldiers in a certain camp exceeds half of the total number of soldiers in all camps, the soldiers of the camp will take away the soldiers from the other camp. In the end, the soldiers of the camp will be left, and the camp will win.

After the war, the soldiers enter the battlefield one by one, first 1 and 2 die together, then 3 comes to the battlefield, now 3 is the winner, then 2 enters and ends with it, 2 enters again... repeat the process. In the end, the only ones left on the battlefield will be the same camp, and that camp will be the one with more than half the number of people.

public class Solution {
    
    
    public int MoreThanHalfNum_Solution(int [] array) {
    
    
        int count = 0; //用来记录当前胜方人数
        int win = 0; //用来记录当前胜方
        for(int n : array){
    
    
            if(count == 0){
    
    
                win = n;
                count = 1;
            }else{
    
    
                if(win == n){
    
     //同一阵营
                    count++;
                }else{
    
     //不同阵营
                    count--;
                }
            }
        }
        int sign = 0;
        for(int n : array){
    
    
            if(n == win){
    
    
                sign = sign+1;
            }
            if(sign >= array.length/2 + 1){
    
    
                return win;
            }
        }
        return 0;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43957211/article/details/114699062