Skip to content

Instantly share code, notes, and snippets.

@spreered
Created September 13, 2017 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spreered/e9dd1362a1abca4e452fbc8168ad21a0 to your computer and use it in GitHub Desktop.
Save spreered/e9dd1362a1abca4e452fbc8168ad21a0 to your computer and use it in GitHub Desktop.
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