Skip to content

Instantly share code, notes, and snippets.

@spreered

spreered/Majority_Element1.rb Secret

Created Sep 13, 2017
Embed
What would you like to do?
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
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