Skip to content

Instantly share code, notes, and snippets.

@piontekle
Last active October 3, 2019 22:01
Show Gist options
  • Save piontekle/ed7bdacfe86e613a2608bcbfd30ba526 to your computer and use it in GitHub Desktop.
Save piontekle/ed7bdacfe86e613a2608bcbfd30ba526 to your computer and use it in GitHub Desktop.
=begin
Given an array, find the int that appears an odd number of times.
There will always be only one integer that appears an odd number of times.
967 ms
=end
def find_it(seq)
#your code here
counts = Hash.new(0)
seq.each { |num| counts[num] += 1 }
counts.each_key { |key| return key if counts[key] % 2 != 0 }
end
#814ms
def find_it(seq)
seq.find { |n| seq.count(n).odd? }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment