-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority element.cpp
More file actions
40 lines (39 loc) · 827 Bytes
/
majority element.cpp
File metadata and controls
40 lines (39 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
int majorityElement(vector<int>& nums) {
int i,l,k=0,j,max,lmt,p;
l=nums.size();
p=l/2+2;
int temp[2][p];
for(i=0;i<p;i++)
temp[1][i]=0;
temp[0][0]=nums[0];
k=1;
for(i=0;i<l;i++)
{
for(j=0;j<k;j++)
{
if(temp[0][j]==nums[i])
{
temp[1][j]++;
break;
}
}
if(j==k)
{
temp[0][k]=nums[i];
temp[1][k]=1;
k++;
}
}
for(i=0;i<k;i++)
{
if(temp[1][i]>l/2)
{
max=temp[0][i];
break;
}
}
return max;
}
};