-
-
Save spreered/e9dd1362a1abca4e452fbc8168ad21a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def majority_element(nums) | |
countNum = Hash.new(0) | |
nums.each do |i| | |
countNum[i] += 1 | |
end | |
ind=-1 | |
val=0 | |
countNum.each do|index, value| | |
puts index | |
puts value | |
if (value > val) | |
ind=index | |
val = value | |
end | |
end | |
return ind | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def majority_element(nums) | |
major = nil | |
count = 0 | |
nums.each do |i| | |
if count == 0 | |
major = i | |
count += 1 | |
else | |
if i == major | |
count+=1 | |
else | |
count -=1 | |
end | |
end | |
end | |
return major | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment