Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 18, 2016 07:17
Show Gist options
  • Save jianminchen/7406702476e8fe7b58934fccb9a4e674 to your computer and use it in GitHub Desktop.
Save jianminchen/7406702476e8fe7b58934fccb9a4e674 to your computer and use it in GitHub Desktop.
Majority - facebook code lab - study the code provided by facebook code lab.
class Solution {
public:
int majorityElement(vector<int> &num) {
int majorityIndex = 0;
for (int count = 1, i = 1; i < num.size(); i++) {
num[majorityIndex] == num[i] ? count++ : count--;
if (count == 0) {
majorityIndex = i;
count = 1;
}
}
return num[majorityIndex];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment