Skip to content

Bug Report for majority-element-ii #5921

@mohneeshdaksh

Description

@mohneeshdaksh

Bug Report for https://neetcode.io/problems/majority-element-ii

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

I wrote the following solution, it passed all the 25 test cases and I successfully submitted my solution:
class Solution:
def majorityElement(self, nums: List[int]) -> List[int]:
count1 = 0
count2 = 0
element1 = None
element2 = None
for current_element in nums:
if element1 is None:
element1 = current_element
count1 = 1
elif element2 is None and current_element != element1:
element2 = current_element
count2 = 1
elif current_element == element1:
count1 += 1
elif current_element == element2:
count2 += 1
elif current_element != element1 and current_element != element2:
count1 -= 1
count2 -= 1
if count1 == 0:
element1 = None
if count2 == 0:
element2 = None
min_count = len(nums) // 3
actual_count1 = nums.count(element1)
actual_count2 = nums.count(element2)
ans = []
if actual_count1 > min_count:
ans.append(element1)
if actual_count2 > min_count:
ans.append(element2)
return ans

BUT this version of my code is actually failing and giving wrong output for test cases similar to the following:
nums = [2,1,1,3,1,4,5,6]
Output = []
Expected = [1]

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