# 169. Majority Element (Easy)

## 1.問題

* 給予一個大小為n的array, 找出出現次數大於一半的成員

![](/files/-LIKQNsSDbw6Rs04Nc9O)

## 2.想法

* 用多個vector儲存數字所出現的次數, 如果次數出現數量超過一半則回傳該數字

## 3.程式碼

```
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        map<int, int> cnt;
        for(int i = 0; i < nums.size(); i++)
        {
            cnt[nums[i]]++;
            if (cnt[nums[i]] > nums.size()/2)
            {
                return nums[i];
            }
        }
    }
};
```

## 4.Performance

![](/files/-LIKQqoB128l5K25ZjWm)


---

# 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://jenhsuan.gitbook.io/algorithm/leetcode/169.-majority-element-easy.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.
