Skip to content

Commit f773c75

Browse files
authored
Merge pull request csubhasundar#141 from bhaveshgarg2000/bhavesh
Majority Elements
2 parents 1070176 + 6705b62 commit f773c75

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

c++/MajorityElement.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution
2+
{
3+
public:
4+
int majorityElement(vector<int> &nums)
5+
{
6+
int cnt = 0;
7+
int element;
8+
int n = nums.size();
9+
for (int i = 0; i < n; i++)
10+
{
11+
if (cnt == 0)
12+
{
13+
element = nums[i];
14+
}
15+
if (nums[i] == element)
16+
{
17+
cnt++;
18+
}
19+
else
20+
{
21+
cnt--;
22+
}
23+
}
24+
return element;
25+
}
26+
};

0 commit comments

Comments
 (0)