# My c solution 10ms

• ``````int majorityElement(int num[], int n) {
int cnt = 0, res;
for (int i = 0; i < n; ++i) {
if (cnt == 0) res = num[i];
if (res == num[i]) ++cnt;
else --cnt;
}
return res;
}``````

• Brilliant! Constant space solution.

• Cool, this is more elegant and much faster than the map way.
You take advantage of the definition of majority!

• This is very nice solution to the given problem, nice thinking

• if the majority element exists for sure, why need cnt?
i think as long as (previous element == current element), we can say that we find the element.

• It is not ordered.

• Not necessary

• this is a brilliant solution. one drawback though: this algorithm cannot tell if the majority actually exists.

• The question assumes that the majority always exists. Anyway, you can check that if res is the majority by traversing the array one more time, it's still O(n) time.

• easy and cool solution

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.