# Majority Number II

题目：

Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array.

分析：

3个数不一样就扔掉, 出现一个不一样的数，用新数抵消掉前俩数,注意最后还需要一个loop 比较2个candidates.

解法：

```
    public int majorityNumber(ArrayList<Integer> nums) {

        // write your code
        int count1 = 0, count2 = 0, c1 = -1, c2 = -1;
        for(int i = 0; i < nums.size(); i++){
            if(nums.get(i) == c1){
                count1++;
            }else if(nums.get(i) == c2){
                count2++;
            }else if(count1 == 0){
                c1 = nums.get(i);
                count1 = 1;
            }else if(count2 == 0){
                c2 = nums.get(i);
                count2 = 1;
            }else {
                count1--;
                count2--;
            }
        }
        //可能c1已经用来抵消掉前面很多数，所以count1<count2 所以要重新loop比较一次。
        count1 = count2 = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums.get(i) == c1) {
                count1++;
            } else if (nums.get(i) == c2) {
                count2++;
            }
        }    
        return count1 > count2 ? c1 : c2;
    }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/high-frequency/majority-number-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
