Find Majority Elements

This code works in 1 case and was giving an error in other test cases. there seems to be an error with the logic please help me out
Thanks