Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25d05b0

Browse files
committed
maj ele
1 parent 20f2d60 commit 25d05b0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int majorityElement(int[] nums) {
3+
int res = 0, count = 0;
4+
for (int ele : nums) {
5+
if (count == 0)
6+
res = ele;
7+
if (res == ele)
8+
count++;
9+
else
10+
count--;
11+
}
12+
return res;
13+
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Given an array nums of size n, return the majority element.
2+
3+
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
4+
5+
6+
```
7+
8+
Example 1:
9+
10+
Input: nums = [3,2,3]
11+
Output: 3
12+
Example 2:
13+
14+
Input: nums = [2,2,1,1,1,2,2]
15+
Output: 2
16+
17+
18+
Constraints:
19+
20+
n == nums.length
21+
1 <= n <= 5 * 104
22+
-109 <= nums[i] <= 109

0 commit comments

Comments
 (0)