Wrong Answer Report

if you create a test case [1,1,1,2,2,2,3] and return 3, submission is accepted.

I think this is because leetcode use this algorithm(I copy and paste from geeksforgeeks website) to test the code:

findCandidate(a[], size)

- Initialize index and count of majority element

maj_index = 0, count = 1 - Loop for i = 1 to size – 1

(a) If a[maj_index] == a[i]

count++

(b) Else

count--;

(c) If count == 0

maj_index = i;

count = 1 - Return a[maj_index]

However, the correct answer is no majority number:

after you finish the algorithm above, you need to loop trough the nums array, check if the number of a[maj_index] is greater than floor(n/2), if it is bigger than floor(n/2) return this number, otherwise return no answer.