-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathMaximum_freq.py
25 lines (23 loc) · 1.06 KB
/
Maximum_freq.py
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
class Solution {
public int majorityElement(int[] nums) {
int candidate = nums[0]; // Initialize the candidate as the first element
int count = 1; // Initialize the count of the current candidate as 1
// Iterate through the array starting from the second element
for (int i = 1; i < nums.length; i++) {
if (nums[i] == candidate) {
// If the current element is the same as the candidate, increment the count
count++;
} else if (count > 0) {
// If the current element is different from the candidate and count is positive,
// decrement the count since we have a matching pair (candidate vs. current element)
count--;
} else {
// If count becomes zero, update the candidate to the current element and set count to 1
candidate = nums[i];
count = 1;
}
}
// At the end, the candidate will be the majority element
return candidate;
}
}