Skip to content

Bug Report for majority-element #4286

Open
@RonitJ7

Description

@RonitJ7

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions