Open
Description
Bug Report for https://neetcode.io/problems/majority-element
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
Same code passes on NC , but does not pass on LC.
The code that passes on NC:
class Solution {
public:
int majorityElement(vector& nums) {
//majority element method
int ans = nums[0];
int count = 1;
for(int i = 1;i<nums.size();i++){
if(nums[i] == ans){
count++;
}else count--;
if(count < 0)
ans = nums[i];
}
return ans;
}
};
It fails on this tc on lc:
nums = [10,9,9,9,10]
Metadata
Metadata
Assignees
Labels
No labels