# 【leetcode】Majority Element

### Question

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

### Code

#include<iostream>
#include<map>
#include<vector>
using namespace std;

class Solution {
public:
int majorityElement(vector<int>& nums) {
map<int, int> count;
int n = nums.size();
for (int i = 0; i < n; i++) {
count[nums[i]]++;
}
for (map<int, int>::iterator it = count.begin(); it != count.end(); it++) {
if (it->second >= (n+1)/ 2) {
return it->first;
}
}
return -1;
}
};

int main() {
Solution so;
vector<int> nums = {1,3,2,2};
cout << so.majorityElement(nums) << endl;
system("pause");
return  0;
}

#### leetcode 229: Majority Element II

2015-07-07 04:40:19

#### 【LeetCode-面试算法经典-Java实现】【169-Majority Element（主元素）】

2015-08-23 06:38:30

#### LeetCode169:Majority Element

2015-07-09 19:29:49

#### LeetCode229:Majority Element II

2015-07-29 15:13:11

#### 169. Majority Element [easy] (Python)

2016-05-14 20:04:32

#### LeetCode 229. Majority Element II（众数II）

2016-04-05 06:28:28

#### [leetcode-169]Majority Element(java)

2015-08-23 11:05:36

#### LeetCode 169. Majority Element 题解（C++）

2016-10-19 14:41:46

#### 【LeetCode】Majority Element 解题报告

2016-05-01 00:02:10

#### LeetCode169. Majority Element分治法

2016-09-21 00:07:18