> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Majority Element II Python Solution with Tests

> Tested Python solution for LeetCode 229 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 229, Medium. Topics: Array, Hash Table, Sorting, Counting. [View on LeetCode](https://leetcode.com/problems/majority-element-ii/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 229   # by problem number
lcpy gen -s majority_element_ii   # by problem name
```

## Problem

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

### Examples

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

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

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

### Constraints

* 1 \<= nums.length \<= 5 \* 10^4
* -10^9 \<= nums\[i] \<= 10^9

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/majority_element_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/majority_element_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def majority_element(self, nums: list[int]) -> list[int]:
        candidate1 = 0
        candidate2 = 0
        count1 = 0
        count2 = 0

        for num in nums:
            if candidate1 == num:
                count1 += 1
            elif candidate2 == num:
                count2 += 1
            elif count1 == 0:
                candidate1 = num
                count1 = 1
            elif count2 == 0:
                candidate2 = num
                count2 = 1
            else:
                count1 -= 1
                count2 -= 1

        threshold = len(nums) // 3
        result: list[int] = []
        if nums.count(candidate1) > threshold:
            result.append(candidate1)
        if candidate2 != candidate1 and nums.count(candidate2) > threshold:
            result.append(candidate2)

        return result
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
