Skip to content

Instantly share code, notes, and snippets.

@woodRock
Created September 18, 2018 05:00
Show Gist options
  • Save woodRock/869b08e2a722468289be92f3dd318673 to your computer and use it in GitHub Desktop.
Save woodRock/869b08e2a722468289be92f3dd318673 to your computer and use it in GitHub Desktop.
Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer. For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19.
#!/usr/bin/env ruby
def non_duplicate_in_set set
freq = set.inject(Hash.new(0)) {|hash,word| hash[word] += 1; hash }
freq.keys.each { |k| return k if freq[k] < 3 }
end
set = [6, 1, 3, 3, 3, 6, 6]
puts non_duplicate_in_set(set)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment