8000 sorting · shivamkumar177/LeetcodeMay@2c46276 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c46276

Browse files
sorting
1 parent 3a80744 commit 2c46276

File tree

1 file changed

+22
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)
0