The first time I read the problem, the straightforward solution that comes to my mind is using a Hash Map.
However, at the end of the question, we see this line:
Follow-up: Could you solve the problem in linear time and in O(1) space?
A Hash Map requires O(n) space, so it’s definitely not the best answer. After racking my brain, I gave up, haha. Then I discovered Moore’s Voting Algorithm.
The main idea of the algorithm is: if the majority element does exist, then its count will be greater than 2n, so count−(n−count)>0
169_majority_element.rb
# frozen_string_literal: true
require'minitest/autorun'
# @param {Integer[]} nums
# @return {Integer}
defmajority_element(nums)
counts={}
max_count=0
result=nil
nums.eachdo|n|
counts[n]||=0
counts[n]+=1
if counts[n]> max_count
max_count= counts[n]
result= n
end
end
if max_count > nums.length/2
result
else
-1
end
end
# Moore's Voting Algorithm
# The idea is: If candidate is the most majority element (count > n/2) => count - (n - count) > 0