> For the complete documentation index, see [llms.txt](https://jaywin.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jaywin.gitbook.io/leetcode/solutions/0229-majority-element-ii.md).

# 0229. Majority Element II

<https://leetcode.com/problems/majority-element-ii>

## Description

Given an integer array of size `n`, find all elements that appear more than `⌊ n/3 ⌋` times.

**Follow-up:** Could you solve the problem in linear time and in O(1) space?

**Example 1:**

```
**Input:** nums = [3,2,3]
**Output:** [3]
```

**Example 2:**

```
**Input:** nums = [1]
**Output:** [1]
```

**Example 3:**

```
**Input:** nums = [1,2]
**Output:** [1,2]
```

**Constraints:**

* `1 <= nums.length <= 5 * 104`
* `-109 <= nums[i] <= 109`

## ac1 Boyer-Moore Algorithm

```java
class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> res = new ArrayList<Integer>();
        // corner case
        if (nums.length == 0) return res;

        // at most 2 majority. pass1: filter out majorities. pass2: recount numbers
        // pass1: filter out majorities
        int m1 = nums[0], m2 = nums[0], cnt1 = 0, cnt2 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == m1) {
                cnt1++;
            } else if (nums[i] == m2) {
                cnt2++;
            } else if (cnt1 == 0) {
                m1 = nums[i];
                cnt1++;
            } else if (cnt2 == 0) {
                m2 = nums[i];
                cnt2++;
            } else {
                cnt1--;
                cnt2--;
            }
        }

        // pass2
        cnt1 = 0; cnt2 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == m1) cnt1++;
            else if (nums[i] == m2) cnt2++;
        }

        // Determine
        if (cnt1 > nums.length/3) res.add(m1);
        if (cnt2 > nums.length/3) res.add(m2);

        return res;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://jaywin.gitbook.io/leetcode/solutions/0229-majority-element-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.
