# [169\. Majority Element](https://leetcode.com/problems/majority-element/)
:::spoiler Hint - Sort
```cpp=
class Solution {
public:
int majorityElement(vector<int>& nums)
{
// Sort the array in non-decreasing order
// The majority element is guaranteed to be at the middle of the sorted array
// due to the definition of majority element (appears more than n/2 times)
}
};
```
:::
:::spoiler Solution - Sort
```cpp=
class Solution {
public:
int majorityElement(vector<int>& nums)
{
sort(nums.begin(), nums.end());
return nums[nums.size() / 2];
}
};
```
- 時間複雜度:$O(\log N)$
- 空間複雜度:$O(1)$
:::