We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 215e67d commit 568b383Copy full SHA for 568b383
majorityElement.cpp
@@ -0,0 +1,25 @@
1
+#include<vector>
2
+using namespace std;
3
+
4
+class Solution {
5
+public:
6
+ int calElementCount(const vector<int>& nums, int element){
7
+ int res = 0;
8
+ for(int e : nums){
9
+ if(e == element)
10
+ res++;
11
+ }
12
+ return res;
13
14
+ int majorityElement(vector<int>& nums) {
15
+ int majorityCount = nums.size() / 2;
16
17
+ while (true)
18
+ {
19
+ int index = rand() % nums.size();
20
+ if(calElementCount(nums, nums[index]) > majorityCount)
21
+ return nums[index];
22
23
24
25
+};
0 commit comments