Java implementation finds out more than half of the number in the array-----(notes)

Java implementation finds out more than half of the number in the array-----(notes)

public class Test {
    
    
    public int majorityElement(int[] nums) {
    
    
        int count = 0;
        int candidate = 0;
        for(int n : nums){
    
    
            if(count == 0) candidate = n;
            if(candidate == n){
    
    
                count++;
            }else{
    
    
                count--;
            }
        }
        return candidate;
    }
}

Guess you like

Origin blog.csdn.net/qq_41454682/article/details/113108325